This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
236 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,72 @@ | ||
# Identity Token | ||
|
||
> Validates and decodes access tokens issued by Sign in with BitGo | ||
Validates and decodes access tokens issued by Sign in with BitGo | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @bitgo/identity-token | ||
``` | ||
|
||
## Usage | ||
|
||
### Decoding JWT | ||
|
||
Decode a JWT payload synchronously and validate its schema. If schema does not | ||
much, an error is thrown. | ||
|
||
> Signature is not verified when decoding, this is useful in client applications since network calls are not made. | ||
```typescript | ||
import { decodeIdentityToken } from "@bitgo/identity-token"; | ||
|
||
const identityToken = decodeIdentityToken(bearerToken); | ||
|
||
if (identityToken.isExpired()) { | ||
throw new Error("Token is expired"); | ||
} | ||
|
||
// shortcut properties | ||
identityToken.userId; | ||
identityToken.enterprises; | ||
|
||
// entire jwt payload is also available | ||
identityToken.payload; | ||
``` | ||
|
||
### Verifying JWT | ||
|
||
Verify a JWT signature was signed by BitGo and decode the JWT payload if verified. | ||
|
||
> Backend services needing authorization should use this method. | ||
```typescript | ||
import { | ||
getIdentityJWKSetFunction, | ||
verifyIdentityToken, | ||
} from "@bitgo/identity-token"; | ||
|
||
// fetches public certs from BitGo to verify signature when invoked | ||
const identityJWKSetFunction = getIdentityJWKSetFunction(); | ||
let identityToken; | ||
try { | ||
identityToken = await verifyIdentityToken( | ||
bearerToken, | ||
identityJWKSetFunction | ||
); | ||
} catch (error) { | ||
// token is either expired, failed to decode, or signature does not match | ||
throw error; | ||
} | ||
|
||
// Example Usage | ||
if (!identityToken.isOriginAllowed(req.header.origin)) { | ||
throw new Error("Request origin is not allowed"); | ||
} | ||
|
||
if (!identityToken.hasScope("required_scope")) { | ||
throw new Error("Token does not contain required scope"); | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { assert } from 'chai'; | ||
|
||
import { decodeIdentityToken } from '../src'; | ||
|
||
describe('Decode Identity Token', () => { | ||
it('should return an identity token given a valid jwt', async () => { | ||
const bearerToken = | ||
'eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI1dTJqaHZQMGkyeU80a0lFUG96ejU5TW40RG4yR0VTc1hqTHg1Z2QxQmp3In0.eyJleHAiOjE4NTIyMTgyODcsImlhdCI6MTY3OTQxODI4NywiYXV0aF90aW1lIjoxNjc5NDE4Mjg3LCJqdGkiOiIwYzZkNzNlZC03MjhhLTQwMWYtOGJiOS05YWU0OWU5YWJmN2YiLCJpc3MiOiJodHRwczovL2lkZW50aXR5LmJpdGdvLWRldi5jb20vcmVhbG1zL2JpdGdvIiwic3ViIjoiZjoxMjY2ZWNmNy1kMzZiLTQ2NGEtOThiZi1kODBjZjE0YWZiNDg6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T2ahVwKoBPdT2z8unIvQ_Gsz-tHWmQyZ95HKt5Lja82lJvS0K2aRhCcTSF1Zw3AGLeaMesl7umMQLkIf5s4aN380Tyx1FeJReVF8dM1_bAvRzrffZQSOUFACU2Qd4LJ2JYaPrIrPLZkDOJ0vQzfBCOsRox-Y6m29oQ6Lw8-hbuN1gtk-DUkMX8AdWto4f74T0d0mKIN929-GYmmriieuqnrk5HqZ7blYrF3GB6jF8-eD5GJe3nhLJAEZ9OJVzKJS6fGyL6zgK-HyZsPg'; | ||
|
||
const identityToken = decodeIdentityToken(bearerToken); | ||
|
||
assert.isDefined(identityToken); | ||
assert.isNotEmpty(identityToken?.payload); | ||
}); | ||
|
||
it('should throw an error given a valid jwt but with an invalid payload schema', async () => { | ||
const bearerToken = | ||
'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.NHVaYe26MbtOYhSKkoKYdFVomg4i8ZJd8_-RU8VNbftc4TSMb4bXP3l3YlNWACwyXPGffz5aXHc6lty1Y2t4SWRqGteragsVdZufDn5BlnJl9pdR_kdVFUsra2rWKEofkZeIC4yWytE58sMIihvo9H1ScmmVwBcQP6XETqYd0aSHp1gOa9RdUPDvoXQ5oqygTqVtxaDr6wUFKrKItgBMzWIdNZ6y7O9E0DhEPTbE9rfBo6KTFsHAZnMg4k68CDp2woYIaXbmYTWcvbzIuHO7_37GT79XdIwkm95QJ7hYC9RiwrV7mesbY4PAahERJawntho0my942XheVLmGwLMBkQ'; | ||
|
||
try { | ||
decodeIdentityToken(bearerToken); | ||
assert.fail(); | ||
} catch (err) {} | ||
}); | ||
|
||
it('should throw an error given an invalid jwt', async () => { | ||
const bearerToken = | ||
'v2x0b75a97dd8caf93b94c0739c8f66478f841217f4ddad7a3cf2e68d2e6a8c5805'; | ||
|
||
try { | ||
decodeIdentityToken(bearerToken); | ||
assert.fail(); | ||
} catch (err) {} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.