|
| 1 | +/** |
| 2 | + * Local dependencies |
| 3 | + */ |
| 4 | +const { JWT } = require('@solid/jose') |
| 5 | +const { random } = require('./crypto') |
| 6 | +const { jwkThumbprintByEncoding } = require('jwk-thumbprint') |
| 7 | + |
| 8 | +const DEFAULT_MAX_AGE = 1209600 // Default Access token expiration, in seconds |
| 9 | +const DEFAULT_SIG_ALGORITHM = 'RS256' |
| 10 | + |
| 11 | +/** |
| 12 | + * DpopAccessToken |
| 13 | + */ |
| 14 | +class DpopAccessToken extends JWT { |
| 15 | + /** |
| 16 | + * issue |
| 17 | + * |
| 18 | + * Creates an access token issued by a given provider. Useful for integration |
| 19 | + * testing of client app code, to generate access tokens to pass along to |
| 20 | + * `supertest` http requests. Usage (in mocha `beforeEach()`, for example): |
| 21 | + * |
| 22 | + * ``` |
| 23 | + * // This assumes you have a Provider instance, an RP Client id, |
| 24 | + * // and a particular user in mind (for the `sub`ject claim) |
| 25 | + * |
| 26 | + * let options = { aud: rpClientId, sub: userId, scope: 'openid profile' } |
| 27 | + * let token = DpopAccessToken.issue(provider, options) |
| 28 | +
|
| 29 | + * return token.encode() |
| 30 | + * .then(compactToken => { |
| 31 | + * headers['Authorization'] = 'Bearer ' + compactToken |
| 32 | + * }) |
| 33 | + * ``` |
| 34 | + * |
| 35 | + * @param options {Object} |
| 36 | + * |
| 37 | + * Required: |
| 38 | + * @param provider {Provider} OIDC Identity Provider issuing the token |
| 39 | + * @param provider.issuer {string} Provider URI |
| 40 | + * @param provider.keys |
| 41 | + * |
| 42 | + * @param options.aud {string|Array<string>} Audience for the token |
| 43 | + * (such as the Relying Party client_id) |
| 44 | + * @param options.sub {string} Subject id for the token (opaque, unique to |
| 45 | + * the issuer) |
| 46 | + * @param options.scope {string} OAuth2 scope |
| 47 | + * |
| 48 | + * Optional: |
| 49 | + * @param [options.alg] {string} Algorithm for signing the access token |
| 50 | + * @param [options.jti] {string} Unique JWT id (to prevent reuse) |
| 51 | + * @param [options.iat] {number} Issued at timestamp (in seconds) |
| 52 | + * @param [options.max] {number} Max token lifetime in seconds |
| 53 | + * |
| 54 | + * @returns {JWT} Access token (JWT instance) |
| 55 | + */ |
| 56 | + static issue (provider, options) { |
| 57 | + const { issuer, keys } = provider |
| 58 | + |
| 59 | + const { aud, sub, cnf } = options |
| 60 | + |
| 61 | + const alg = options.alg || DEFAULT_SIG_ALGORITHM |
| 62 | + const jti = options.jti || random(8) |
| 63 | + const iat = options.iat || Math.floor(Date.now() / 1000) |
| 64 | + const max = options.max || DEFAULT_MAX_AGE |
| 65 | + |
| 66 | + const exp = iat + max // token expiration |
| 67 | + |
| 68 | + const iss = issuer |
| 69 | + const key = keys.token.signing[alg].privateKey |
| 70 | + const kid = keys.token.signing[alg].publicJwk.kid |
| 71 | + |
| 72 | + |
| 73 | + const header = { alg, kid } |
| 74 | + const payload = { iss, aud, sub, exp, iat, jti, cnf } |
| 75 | + |
| 76 | + const jwt = new DpopAccessToken({ header, payload, key }) |
| 77 | + |
| 78 | + return jwt |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * issue |
| 83 | + */ |
| 84 | + static issueForRequest (request, response) { |
| 85 | + const { params, code, provider, client, subject, defaultRsUri } = request |
| 86 | + |
| 87 | + const alg = client['access_token_signed_response_alg'] || DEFAULT_SIG_ALGORITHM |
| 88 | + const jti = random(8) |
| 89 | + const iat = Math.floor(Date.now() / 1000) |
| 90 | + let aud, sub, max, scope |
| 91 | + |
| 92 | + // authentication request |
| 93 | + if (!code) { |
| 94 | + aud = client['client_id'] |
| 95 | + if (defaultRsUri && defaultRsUri !== client['client_id']) { |
| 96 | + aud.push(defaultRsUri) |
| 97 | + } |
| 98 | + sub = subject['_id'] |
| 99 | + max = parseInt(params['max_age']) || client['default_max_age'] || DEFAULT_MAX_AGE |
| 100 | + scope = request.scope |
| 101 | + |
| 102 | + // token request |
| 103 | + } else { |
| 104 | + aud = [ code.aud ] |
| 105 | + if (defaultRsUri && defaultRsUri !== code.aud) { |
| 106 | + aud.push(defaultRsUri) |
| 107 | + } |
| 108 | + sub = code.sub |
| 109 | + max = parseInt(code['max']) || client['default_max_age'] || DEFAULT_MAX_AGE |
| 110 | + scope = code.scope |
| 111 | + } |
| 112 | + |
| 113 | + const cnf = { |
| 114 | + jkt: jwkThumbprintByEncoding(request.dpopJwk, "SHA-256", 'base64url') |
| 115 | + } |
| 116 | + |
| 117 | + const options = { aud, sub, scope, alg, jti, iat, max, cnf } |
| 118 | + |
| 119 | + let header, payload |
| 120 | + |
| 121 | + return Promise.resolve() |
| 122 | + .then(() => DpopAccessToken.issue(provider, options)) |
| 123 | + .then(jwt => { |
| 124 | + header = jwt.header |
| 125 | + payload = jwt.payload |
| 126 | + |
| 127 | + return jwt.encode() |
| 128 | + }) |
| 129 | + // set the response properties |
| 130 | + .then(compact => { |
| 131 | + response['access_token'] = compact |
| 132 | + response['token_type'] = 'Bearer' |
| 133 | + response['expires_in'] = max |
| 134 | + }) |
| 135 | + // store access token by "jti" claim |
| 136 | + .then(() => { |
| 137 | + return provider.backend.put('tokens', `${jti}`, { header, payload }) |
| 138 | + }) |
| 139 | + // store access token by "refresh_token", if applicable |
| 140 | + .then(() => { |
| 141 | + let responseTypes = request.responseTypes || [] |
| 142 | + let refresh |
| 143 | + |
| 144 | + if (code || responseTypes.includes('code')) { |
| 145 | + refresh = random(16) |
| 146 | + } |
| 147 | + |
| 148 | + if (refresh) { |
| 149 | + response['refresh_token'] = refresh |
| 150 | + return provider.backend.put('refresh', `${refresh}`, { header, payload }) |
| 151 | + } |
| 152 | + }) |
| 153 | + // resolve the response |
| 154 | + .then(() => response) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +DpopAccessToken.DEFAULT_MAX_AGE = DEFAULT_MAX_AGE |
| 159 | +DpopAccessToken.DEFAULT_SIG_ALGORITHM = DEFAULT_SIG_ALGORITHM |
| 160 | + |
| 161 | +/** |
| 162 | + * Export |
| 163 | + */ |
| 164 | +module.exports = DpopAccessToken |
0 commit comments