Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Validate jwt: response token builder accept exp as function arg #324

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b8cae78
fix: did & added validation on expiresAt
Aug 23, 2023
c365a81
fix: did & added validation on expiresAt
Aug 23, 2023
abe53d4
fix: lib version upgrade
Aug 23, 2023
d91c2ff
fix: linter issue
Aug 23, 2023
0a1c6c2
fix: merge conditions
Aug 23, 2023
c34e05c
fix: linter error
Aug 23, 2023
0ad1289
fix: linter error
Aug 23, 2023
3b7c64c
fix: corrected changelog date
Aug 23, 2023
35674b9
fix: condition added to handle undefined exp
Aug 23, 2023
f5873ee
fix: condition added to handle undefined exp
Aug 23, 2023
8fbd8ca
fix: error codes and messages
Aug 24, 2023
8e6c48f
fix: exp for response token
Aug 25, 2023
30bdf07
Merge branch 'master' into validateJWT
mritunjayk2 Aug 25, 2023
f37a3d0
fix: exp for response token
Aug 25, 2023
c5b9502
Merge branch 'validateJWT' of https://github.com/affinidi/affinidi-co…
Aug 25, 2023
dd87e0f
fix: lint issue
Aug 25, 2023
8ea36e2
fix: lint issue
Aug 25, 2023
917f034
feat: unit test for invalid expiration of response token
Aug 29, 2023
49c6057
feat: did auth lib readme for options.exp
Aug 29, 2023
3dd4482
fix: test case
Aug 29, 2023
6e9f85e
fix: test case
Aug 29, 2023
3a2fc23
fix(codeclimate): upgrade versions
oleksiipiliugin Aug 29, 2023
86378c9
fix: assert for test
Aug 29, 2023
ed5592e
Merge branch 'validateJWT' of https://github.com/affinidi/affinidi-co…
Aug 29, 2023
61432e7
fix: assert for test
Aug 29, 2023
9103165
fix: assert for test
Aug 29, 2023
cfd9728
fix: package structure
Aug 29, 2023
093f075
fix: package structure
Aug 29, 2023
bb024dc
fix: did auth service test case
Aug 29, 2023
97f0f41
fix(domain): update default template domain
oleksiipiliugin Aug 29, 2023
d891742
fix: wrong expiration
Aug 29, 2023
d2a20cb
fix: wrong expiration
Aug 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codeclimate-workflow-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
run: npm run build
- name: Lint packages
run: npm run lint
- uses: paambaati/codeclimate-action@v2.7.1
- uses: paambaati/codeclimate-action@v2.6.0
env:
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
TEST_SECRETS: ${{secrets.INTEGRATION_TEST_SECRETS}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeclimate-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: npm run build
- name: Lint packages
run: npm run lint
- uses: paambaati/codeclimate-action@v2.7.1
- uses: paambaati/codeclimate-action@v2.6.0
env:
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
TEST_SECRETS: ${{secrets.INTEGRATION_TEST_SECRETS}}
Expand Down
2 changes: 2 additions & 0 deletions common-libs/did-auth-lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# release 3.0.0 (2023-08-29)
* added expiration in Response Token Options
# release 2.9.0 (2023-03-14)
* add tenant token
# release 2.8.0 (2023-03-08)
Expand Down
1 change: 1 addition & 0 deletions common-libs/did-auth-lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Use `createDidAuthResponseToken` method to create `Did-Auth` response token(expi
* authDidRequestToken {String} - signed JWT request token from the service
* options {Object} (optional) - key value object with additional options
* options.maxTokenValidInMs {Number} (optional) - maximum token validity period in milliseconds(12 hours by default)
* options.exp {Number} (optional) - `responseToken` expiration timestamp
*/
const responseToken = await affinidiDidAuthService.createDidAuthResponseToken(authDidRequestToken, options)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class DidAuthClientService {
)
}

const jwtObject = await buildResponseJwtObject(didAuthRequestTokenStr)
const exp = options?.exp ?? undefined

const jwtObject = await buildResponseJwtObject(didAuthRequestTokenStr, exp)

await this._signer.fillSignature(jwtObject)

Expand Down
4 changes: 2 additions & 2 deletions common-libs/did-auth-lib/src/shared/builder.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { JwtService } from '@affinidi/tools-common'
import { parse } from 'did-resolver'

export const buildResponseJwtObject = async (didAuthRequestToken: string) => {
export const buildResponseJwtObject = async (didAuthRequestToken: string, exp?: number) => {
const didAuthRequestTokenDecoded = JwtService.fromJWT(didAuthRequestToken)
const jwtType = 'DidAuthResponse'
const NOW = Date.now()

const jwtObject: any = await JwtService.buildJWTInteractionToken(null, jwtType, didAuthRequestTokenDecoded)
jwtObject.payload.requestToken = didAuthRequestToken
jwtObject.payload.aud = parse(didAuthRequestTokenDecoded.payload.iss).did
jwtObject.payload.exp = undefined
jwtObject.payload.exp = exp
jwtObject.payload.createdAt = NOW
return jwtObject
}
1 change: 1 addition & 0 deletions common-libs/did-auth-lib/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface VerifierOptions {

export type CreateResponseTokenOptions = {
maxTokenValidInMs?: number
exp?: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Env } from '@affinidi/url-resolver'
import AffinidiDidAuthService from './../../../src/DidAuthService/DidAuthService'
import { verifierEncryptedSeed, verifierEncryptionKey, verifierFullDid, verifierDid } from '../../factory/verifier'
import { holderEncryptedSeed, holderEncryptionKey, holderDid } from '../../factory/holder'
import { DEFAULT_REQUEST_TOKEN_VALID_IN_MS } from './../../../src/shared/constants'
import { CreateResponseTokenOptions } from './../../../src/shared/types'
const { TEST_SECRETS } = process.env
const { DEV_API_KEY_HASH } = JSON.parse(TEST_SECRETS)
const env = {
Expand Down Expand Up @@ -74,7 +76,11 @@ module.exports = function () {

const didAuthRequestToken = await verifierDidAuthService.createDidAuthRequestToken(holderDid)

const didAuthResponseToken = await holderDidAuthService.createDidAuthResponseToken(didAuthRequestToken)
const options: CreateResponseTokenOptions = {
exp: Date.now() + DEFAULT_REQUEST_TOKEN_VALID_IN_MS
}

const didAuthResponseToken = await holderDidAuthService.createDidAuthResponseToken(didAuthRequestToken, options)

const result = await verifierDidAuthService.verifyDidAuthResponseToken(didAuthResponseToken, verifierOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import DidAuthServerService from '../../../src/DidAuthService/DidAuthServerServi
import Signer from '../../../src/shared/Signer'
import { Affinidi, KeysService, LocalKeyVault } from '@affinidi/common'
import DidAuthClientService from '../../../src/DidAuthService/DidAuthClientService'
import { DEFAULT_REQUEST_TOKEN_VALID_IN_MS } from './../../../src/shared/constants'
import { CreateResponseTokenOptions } from './../../../src/shared/types'

const env = {
environment: <Env>'dev',
Expand Down Expand Up @@ -150,7 +152,11 @@ describe('AffinidiDidAuthService', () => {

const didAuthRequestToken = await verifierDidAuthService.createDidAuthRequestToken(holderDid)

const didAuthResponseToken = await holderDidAuthService.createDidAuthResponseToken(didAuthRequestToken)
const options: CreateResponseTokenOptions = {
exp: Date.now() + DEFAULT_REQUEST_TOKEN_VALID_IN_MS
}

const didAuthResponseToken = await holderDidAuthService.createDidAuthResponseToken(didAuthRequestToken, options)

const result = await verifierDidAuthService.verifyDidAuthResponseToken(didAuthResponseToken, verifierOptions)

Expand All @@ -174,7 +180,11 @@ describe('AffinidiDidAuthService', () => {

const didAuthRequestToken = await serverService.createDidAuthRequestToken(holderDid)

const didAuthResponseToken = await clientService.createDidAuthResponseToken(didAuthRequestToken)
const options: CreateResponseTokenOptions = {
exp: Date.now() + DEFAULT_REQUEST_TOKEN_VALID_IN_MS
}

const didAuthResponseToken = await clientService.createDidAuthResponseToken(didAuthRequestToken, options)

const result = await serverService.verifyDidAuthResponseToken(didAuthResponseToken)

Expand Down Expand Up @@ -267,4 +277,38 @@ describe('AffinidiDidAuthService', () => {

expect(() => holderDidAuthService.isTokenExpired(token)).to.throw()
})

it('#verifyDidAuthResponse -> invalid expiration for didAuthResponseToken ', async () => {
const { environment, accessApiKey } = env

nock(`https://affinity-registry.apse1.${environment}.affinidi.io`)
.post('/api/v1/did/resolve-did', /elem/gi)
.reply(200, mockVerifierElemDidDocument)

nock(`https://affinity-registry.apse1.${environment}.affinidi.io`)
.post('/api/v1/did/resolve-did', /elem/gi)
.reply(200, mockHolderElemDidDocument)

const clientService = createClientService()
const serverService = createServerService(environment, accessApiKey)

const didAuthRequestToken = await serverService.createDidAuthRequestToken(holderDid)

const options: CreateResponseTokenOptions = { exp: 42 }

const didAuthResponseToken = await clientService.createDidAuthResponseToken(didAuthRequestToken, options)

let invalidExpirationError
try {
console.log(didAuthResponseToken)
await serverService.verifyDidAuthResponseToken(didAuthResponseToken)
} catch (error) {
console.log(error.message)
invalidExpirationError = error
}

expect(invalidExpirationError).to.be.not.undefined
expect(invalidExpirationError.message).to.be.equal('Token expired or invalid expiration')
nock.cleanAll()
})
})
2 changes: 1 addition & 1 deletion common-libs/url-resolver/src/templates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Service } from './services'

export const defaultTemplate = 'https://{{service}}.{{env}}.affinity-project.org'
export const defaultTemplate = 'https://{{service}}.apse1.affinidi.io'

export const defaultDevTemplate = 'https://{{service}}.apse1.{{env}}.affinidi.io'

Expand Down
Loading
Loading