@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
119 lines • 4.92 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
const Identifier_1 = require("../Identifier");
const OidcResponse_1 = require("./OidcResponse");
const CryptoOptions_1 = require("../CryptoOptions");
const JwsToken_1 = require("../crypto/protocols/jose/jws/JwsToken");
const UserAgentOptions_1 = require("../UserAgentOptions");
const HttpResolver_1 = require("../resolvers/HttpResolver");
/**
* Standard response type for SIOP.
*/
const responseType = 'id_token';
/**
* Standard response mode for SIOP.
*/
const responseMode = 'form_post';
/**
* Standard scope for SIOP.
*/
const scope = 'openid did_authn';
/**
* Class to represent Open ID Connect Self-Issued Tokens
* @class
*/
class OidcRequest {
/**
* Instantiates an self-signed OIDC Request.
* @param sender Identifier who will sign request.
* @param redirectUrl Redirect URL for SIOP.
* @param nonce Nonce for SIOP.
* @param claimObject optional claimObject to attach to request.
*/
constructor(sender, redirectUrl, nonce, options) {
this.sender = sender;
this.redirectUrl = redirectUrl;
this.nonce = nonce;
this.claimObject = options.claimObject;
this.state = options.state;
this.claimsRequested = options.claimsRequested;
}
/**
* Forms the request to spec and sign the request.
* @param keyReference
* @returns jwt in compact form.
*/
async sign(keyReference) {
const request = {
iss: this.sender.id,
response_type: responseType,
response_mode: responseMode,
client_id: this.redirectUrl,
scope,
nonce: this.nonce
};
if (this.state) {
Object.assign(request, { state: this.state });
}
if (this.claimsRequested) {
const claimsRequestIdToken = {};
// Assuming every claim requested is essential for now.
this.claimsRequested.forEach(claimRef => {
claimsRequestIdToken[claimRef] = {
essential: true
};
});
Object.assign(request, { claims: { id_token: claimsRequestIdToken } });
}
if (this.claimObject) {
Object.assign(request, { offer: JSON.parse(this.claimObject.serialize()) });
}
return this.sender.sign(request, keyReference);
}
/**
* Parses and Verifies signed JWT.
* @param signedRequest signed JWT containing OIDC request.
* @returns OidcRequest Object if verified.
*/
static async verifyAndParse(signedRequest, cryptoFactory) {
if (!cryptoFactory) {
cryptoFactory = new CryptoOptions_1.default().cryptoFactory;
}
// get identifier id from key id in header.
const token = await JwsToken_1.default.deserialize(signedRequest, { cryptoFactory });
const request = JSON.parse(token.payload.toString());
const senderId = request.iss;
const options = new UserAgentOptions_1.default();
options.resolver = new HttpResolver_1.default('https://beta.discover.did.microsoft.com');
const sender = new Identifier_1.default(senderId, options);
// if (!await VerifyHelper.verify(sender, token)) {
console.log('not verifying for now');
// throw new UserAgentError(`Invalid signature for token issued by: ${request.iss}`);
// }
const requestOptions = {
state: request.state,
claimObject: request.offer,
};
// parse out requested claims to list if param is present.
if (request.claims && request.claims.id_token) {
const claimsRequested = Object.keys(request.claims.id_token);
Object.assign(requestOptions, { claimsRequested });
}
return new OidcRequest(sender, request.client_id, request.nonce, requestOptions);
}
/**
* Respond to OIDC Request using identifier on the client side.
* @param identifier the identifier used to sign response
* @returns the body of the HTTP response if receive status 200.
*/
async respondWith(identifier, keyReference, claimObjects) {
const oidcResponse = await OidcResponse_1.default.create(this, identifier);
return oidcResponse.signAndSend(keyReference, claimObjects);
}
}
exports.default = OidcRequest;
//# sourceMappingURL=OidcRequest.js.map