forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoidc_context.js
More file actions
135 lines (105 loc) · 3.37 KB
/
oidc_context.js
File metadata and controls
135 lines (105 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const url = require('url');
const _ = require('lodash');
const uuid = require('uuid/v4');
const debug = require('debug')('oidc-provider:bearer');
const { InvalidRequest } = require('./errors');
module.exports = function getContext(provider) {
const map = new WeakMap();
function instance(ctx) {
if (!map.has(ctx)) map.set(ctx, { claims: {} });
return map.get(ctx);
}
class OIDCContext {
constructor(ctx) {
this.ctx = ctx;
this.route = ctx._matchedRouteName;
this.authorization = {};
this.redirectUriCheckPerformed = false;
this.webMessageUriCheckPerformed = false;
this.uuid = (ctx.params && ctx.params.grant) || uuid();
this.entities = {};
this.claims = {};
this.issuer = provider.issuer;
}
entity(key, value) {
this.entities[key] = value;
}
urlFor(name, opt) {
const mountPath = (this.ctx.req.originalUrl && this.ctx.req.originalUrl.substring(
0,
this.ctx.req.originalUrl.indexOf(this.ctx.request.url),
))
|| this.ctx.mountPath // koa-mount
|| this.ctx.req.baseUrl // expressApp.use('/op', provider.callback);
|| ''; // no mount
return url.resolve(this.ctx.href, provider.pathFor(name, { mountPath, ...opt }));
}
promptPending(name) {
// result pass
if (this.ctx.oidc.route.endsWith('resume')) {
if (name === 'none') return true;
const should = _.difference(this.prompts, Object.keys(this.result || {}));
return should.includes(name);
}
// first pass
return this.prompts && this.prompts.includes(name);
}
get acr() {
return _.get(this, 'result.login.acr');
}
get amr() {
return _.get(this, 'result.login.amr');
}
get prompts() { return this.params.prompt ? this.params.prompt.split(' ') : []; }
get bearer() {
if ('bearer' in instance(this)) {
return instance(this).bearer;
}
const { ctx } = this;
const mechanisms = _.omitBy({
body: _.get(ctx.oidc, 'body.access_token'),
header: ctx.headers.authorization,
query: ctx.query.access_token,
}, _.isUndefined);
debug('uuid=%s received bearer via %o', this.uuid, mechanisms);
let mechanism;
let length;
let bearer;
try {
({ 0: [mechanism, bearer], length } = Object.entries(mechanisms));
} catch (err) {}
if (!length) {
throw new InvalidRequest('no bearer auth mechanism provided');
}
if (length > 1) {
throw new InvalidRequest('bearer token must only be provided using one mechanism');
}
if (mechanism === 'header') {
const header = bearer;
const { 0: scheme, 1: value, length: parts } = header.split(' ');
if (parts !== 2 || scheme !== 'Bearer') {
throw new InvalidRequest('invalid authorization header value format');
}
bearer = value;
}
if (!bearer) {
throw new InvalidRequest('no bearer token provided');
}
instance(this).bearer = bearer;
return bearer;
}
get registrationAccessToken() {
return this.entities.RegistrationAccessToken;
}
get deviceCode() {
return this.entities.DeviceCode;
}
get account() {
return this.entities.Account;
}
get client() {
return this.entities.Client;
}
}
return OIDCContext;
};