oauth3.js/oauth3.issuer.mock.js

106 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-02-20 22:22:48 +00:00
;(function (exports) {
'use strict';
2017-03-02 07:44:14 +00:00
var OAUTH3 = exports.OAUTH3 = exports.OAUTH3 || require('./oauth3.core.js').OAUTH3;
OAUTH3._base64.btoa = function (b64) {
// http://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome
return (exports.btoa || require('btoa'))(b64);
2017-02-22 01:18:14 +00:00
};
OAUTH3._base64.encodeUrlSafe = function (b64) {
2017-02-20 22:22:48 +00:00
// Base64 to URL-safe Base64
b64 = b64.replace(/\+/g, '-').replace(/\//g, '_');
b64 = b64.replace(/=+/g, '');
return OAUTH3._base64.btoa(b64);
2017-02-20 22:22:48 +00:00
};
OAUTH3.jwt.encode = function (parts) {
parts.header = parts.header || { alg: 'none', typ: 'jwt' };
parts.signature = parts.signature || '';
var result = [
OAUTH3._base64.encodeUrlSafe(JSON.stringify(parts.header, null))
, OAUTH3._base64.encodeUrlSafe(JSON.stringify(parts.payload, null))
2017-02-20 22:22:48 +00:00
, parts.signature // should already be url-safe base64
].join('.');
return result;
};
OAUTH3.authn.resourceOwnerPassword = OAUTH3.authz.resourceOwnerPassword = function (directive, opts) {
var providerUri = directive.issuer;
if (opts.mockError) {
return OAUTH3.PromiseA.resolve({data: {error_description: "fake error", error: "errorcode", error_uri: "https://blah"}});
}
return OAUTH3._mockToken(providerUri, opts);
};
2017-02-22 01:18:14 +00:00
OAUTH3.authn.loginMeta = function (directive, opts) {
if (opts.mockError) {
return OAUTH3.PromiseA.resolve({ data: { error: { message: "Yikes!", code: 'E' } } });
}
return OAUTH3.PromiseA.resolve({ data: {} });
};
OAUTH3.authn.otp = function (directive, opts) {
if (opts.mockError) {
return OAUTH3.PromiseA.resolve({data: {error: {message: "Yikes!", code: 'E'}}});
}
return OAUTH3.PromiseA.resolve({data: {uuid: "uuidblah"}});
};
2017-02-20 22:22:48 +00:00
2017-02-22 01:18:14 +00:00
OAUTH3.authz.scopes = function () {
return {
pending: ['oauth3_authn'] // not yet accepted
, granted: [] // all granted, ever
, requested: ['oauth3_authn'] // all requested, now
, accepted: [] // granted (ever) and requested (now)
};
};
2017-02-20 22:22:48 +00:00
OAUTH3.authz.grants = function (providerUri, opts) {
if ('POST' === opts.method) {
return OAUTH3._mockToken(providerUri, opts);
}
return OAUTH3.discover(providerUri, {
client_id: providerUri
, debug: opts.debug
}).then(function (directive) {
return {
client: {
name: "foo"
, client_id: "localhost.foo.daplie.me:8443"
, url: "https://localhost.foo.daplie.me:8443"
}
, grants: []
};
});
};
OAUTH3._refreshToken = function (providerUri, opts) {
return OAUTH3._mockToken(providerUri, opts);
};
OAUTH3._mockToken = function (providerUri, opts) {
var accessToken = OAUTH3.jwt.encode({
header: { alg: 'none' }
, payload: { exp: Math.round(Date.now() / 1000) + 900, sub: 'fakeUserId', scp: opts.scope }
, signature: "fakeSig"
});
2017-02-20 22:22:48 +00:00
return OAUTH3.hooks.session.refresh(
opts.session || {
provider_uri: providerUri
, client_id: opts.client_id
, client_uri: opts.client_uri || opts.clientUri
}
, { access_token: accessToken
, refresh_token: accessToken
, expires_in: "900"
, scope: opts.scope
}
);
};
}('undefined' !== typeof exports ? exports : window));