Skip to content

Commit

Permalink
fix: issuance and expiration sometimes used milliseconds instead of s…
Browse files Browse the repository at this point in the history
…econds
  • Loading branch information
nklomp committed Apr 25, 2024
1 parent 1260291 commit afc2a8a
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function proofOfPossessionCallbackFunction(args: Jwt, kid?: string): Promi
}
return await new jose.SignJWT({ ...args.payload })
.setProtectedHeader({ ...args.header })
.setIssuedAt(+new Date())
.setIssuedAt(args.payload.iat ?? Math.round(+new Date()/1000))
.setIssuer(kid)
.setAudience(args.payload.aud)
.setExpirationTime('2h')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IDENTIPROOF_ISSUER_URL } from './MetadataMocks';

const jwt: Jwt = {
header: { alg: Alg.ES256, kid: 'did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1', typ: 'jwt' },
payload: { iss: 'sphereon:wallet', nonce: 'tZignsnFbp', jti: 'tZignsnFbp223', aud: IDENTIPROOF_ISSUER_URL, iat: Date.now() },
payload: { iss: 'sphereon:wallet', nonce: 'tZignsnFbp', jti: 'tZignsnFbp223', aud: IDENTIPROOF_ISSUER_URL, iat: Date.now()/1000 },
};

const kid = 'did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1';
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/__tests__/SdJwt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const vcIssuer = new VcIssuerBuilder()
},
payload: {
aud: issuerMetadata.credential_issuer,
iat: +new Date(),
iat: +new Date()/1000,
nonce: 'a-c-nonce',
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/client/lib/functions/ProofUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ const createJWT = (jwtProps?: JwtProps, existingJwt?: Jwt): Jwt => {
const now = +new Date();
const jwtPayload: Partial<JWTPayload> = {
aud,
iat: jwt.payload?.iat ? jwt.payload.iat : now / 1000 - 60, // Let's ensure we subtract 60 seconds for potential time offsets
exp: jwt.payload?.exp ? jwt.payload.exp : now / 1000 + 10 * 60,
iat: jwt.payload?.iat ?? Math.round(now / 1000 - 60), // Let's ensure we subtract 60 seconds for potential time offsets
exp: jwt.payload?.exp ?? Math.round(now / 1000 + 10 * 60),
nonce,
...(iss ? { iss } : {}),
...(jti ? { jti } : {}),
Expand Down
4 changes: 2 additions & 2 deletions packages/issuer-rest/lib/IssuerTokenEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { v4 } from 'uuid'
* @param interval
*/
export const handleTokenRequest = <T extends object>({
tokenExpiresIn,
tokenExpiresIn, // expiration in seconds
accessTokenSignerCallback,
accessTokenIssuer,
cNonceExpiresIn,
cNonceExpiresIn, // expiration in seconds
issuer,
interval,
}: Required<Pick<ITokenEndpointOpts, 'accessTokenIssuer' | 'cNonceExpiresIn' | 'interval' | 'accessTokenSignerCallback' | 'tokenExpiresIn'>> & {
Expand Down
2 changes: 1 addition & 1 deletion packages/issuer-rest/lib/__tests__/ClientIssuerIT.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ describe('VcIssuer', () => {
async function proofOfPossessionCallbackFunction(args: Jwt, kid?: string): Promise<string> {
return await new jose.SignJWT({ ...args.payload })
.setProtectedHeader({ ...args.header })
.setIssuedAt(+new Date())
.setIssuedAt(args.payload.iat ?? Math.round(+new Date()/1000))
.setIssuer(kid!)
.setAudience(args.payload.aud!)
.setExpirationTime('2h')
Expand Down
2 changes: 1 addition & 1 deletion packages/issuer/lib/VcIssuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ export class VcIssuer<DIDDoc extends object> {
}
if (!iat) {
throw new Error(IAT_ERROR)
} else if (iat > (createdAt/1000 + tokenExpiresIn)) {
} else if (iat > Math.round(createdAt/1000) + tokenExpiresIn) {
// createdAt is in milliseconds whilst iat and tokenExpiresIn are in seconds
throw new Error(IAT_ERROR)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/issuer/lib/__tests__/VcIssuer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('VcIssuer', () => {
},
payload: {
aud: IDENTIPROOF_ISSUER_URL,
iat: +new Date(),
iat: +new Date()/1000,
nonce: 'test-nonce',
},
},
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('VcIssuer', () => {
},
payload: {
aud: IDENTIPROOF_ISSUER_URL,
iat: +new Date(),
iat: +new Date()/1000,
nonce: 'test-nonce',
},
},
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('VcIssuer', () => {
},
payload: {
aud: IDENTIPROOF_ISSUER_URL,
iat: +new Date(),
iat: +new Date()/1000,
nonce: 'test-nonce',
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/issuer/lib/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ export const createAccessTokenResponse = async (
credentialOfferSessions: IStateManager<CredentialOfferSession>
cNonces: IStateManager<CNonceState>
cNonce?: string
cNonceExpiresIn?: number
tokenExpiresIn: number
cNonceExpiresIn?: number // expiration in seconds
tokenExpiresIn: number // expiration in seconds
// preAuthorizedCodeExpirationDuration?: number
accessTokenSignerCallback: JWTSignerCallback
accessTokenIssuer: string
Expand Down

0 comments on commit afc2a8a

Please sign in to comment.