forked from tngan/samlify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata-sp.ts
209 lines (186 loc) Β· 6.42 KB
/
metadata-sp.ts
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @file metadata-sp.ts
* @author tngan
* @desc Metadata of service provider
*/
import Metadata, { MetadataInterface } from './metadata';
import { MetadataSpConstructor, MetadataSpOptions } from './types';
import { namespace, elementsOrder as order } from './urn';
import libsaml from './libsaml';
import { isNonEmptyArray, isString } from './utility';
import * as xml from 'xml';
export interface SpMetadataInterface extends MetadataInterface {
}
// https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf (P.16, 18)
interface MetaElement {
KeyDescriptor?: any[];
NameIDFormat?: any[];
SingleLogoutService?: any[];
AssertionConsumerService?: any[];
AttributeConsumingService?: any[];
}
/*
* @desc interface function
*/
export default function(meta: MetadataSpConstructor) {
return new SpMetadata(meta);
}
/**
* @desc SP Metadata is for creating Service Provider, provides a set of API to manage the actions in SP.
*/
export class SpMetadata extends Metadata {
/**
* @param {object/string} meta (either xml string or configuation in object)
* @return {object} prototypes including public functions
*/
constructor(meta: MetadataSpConstructor) {
const isFile = isString(meta) || meta instanceof Buffer;
// use object configuation instead of importing metadata file directly
if (!isFile) {
const {
elementsOrder = order.default,
entityID,
signingCert,
encryptCert,
authnRequestsSigned = false,
wantAssertionsSigned = false,
wantMessageSigned = false,
signatureConfig,
nameIDFormat = [],
singleLogoutService = [],
assertionConsumerService = [],
} = meta as MetadataSpOptions;
const descriptors: MetaElement = {
KeyDescriptor: [],
NameIDFormat: [],
SingleLogoutService: [],
AssertionConsumerService: [],
AttributeConsumingService: [],
};
const SPSSODescriptor: any[] = [{
_attr: {
AuthnRequestsSigned: String(authnRequestsSigned),
WantAssertionsSigned: String(wantAssertionsSigned),
protocolSupportEnumeration: namespace.names.protocol,
},
}];
if (wantMessageSigned && signatureConfig === undefined) {
console.warn('Construct service provider - missing signatureConfig');
}
if (signingCert) {
descriptors.KeyDescriptor!.push(libsaml.createKeySection('signing', signingCert).KeyDescriptor);
} else {
//console.warn('Construct service provider - missing signing certificate');
}
if (encryptCert) {
descriptors.KeyDescriptor!.push(libsaml.createKeySection('encryption', encryptCert).KeyDescriptor);
} else {
//console.warn('Construct service provider - missing encrypt certificate');
}
if (isNonEmptyArray(nameIDFormat)) {
nameIDFormat.forEach(f => descriptors.NameIDFormat!.push(f));
} else {
// default value
descriptors.NameIDFormat!.push(namespace.format.emailAddress);
}
if (isNonEmptyArray(singleLogoutService)) {
let indexCount = 0;
singleLogoutService.forEach(a => {
const attr: any = {
index: String(indexCount++),
Binding: a.Binding,
Location: a.Location,
};
if (a.isDefault) {
attr.isDefault = true;
}
descriptors.SingleLogoutService!.push([{ _attr: attr }]);
});
}
if (isNonEmptyArray(assertionConsumerService)) {
let indexCount = 0;
assertionConsumerService.forEach(a => {
const attr: any = {
index: String(indexCount++),
Binding: a.Binding,
Location: a.Location,
};
if (a.isDefault) {
attr.isDefault = true;
}
descriptors.AssertionConsumerService!.push([{ _attr: attr }]);
});
} else {
// console.warn('Missing endpoint of AssertionConsumerService');
}
// handle element order
const existedElements = elementsOrder.filter(name => isNonEmptyArray(descriptors[name]));
existedElements.forEach(name => {
descriptors[name].forEach(e => SPSSODescriptor.push({ [name]: e }));
});
// Re-assign the meta reference as a XML string|Buffer for use with the parent constructor
meta = xml([{
EntityDescriptor: [{
_attr: {
entityID,
'xmlns': namespace.names.metadata,
'xmlns:assertion': namespace.names.assertion,
'xmlns:ds': 'http://www.w3.org/2000/09/xmldsig#',
},
}, { SPSSODescriptor }],
}]);
}
// Use the re-assigned meta object reference here
super(meta as string | Buffer, [
{
key: 'spSSODescriptor',
localPath: ['EntityDescriptor', 'SPSSODescriptor'],
attributes: ['WantAssertionsSigned', 'AuthnRequestsSigned'],
},
{
key: 'assertionConsumerService',
localPath: ['EntityDescriptor', 'SPSSODescriptor', 'AssertionConsumerService'],
attributes: ['Binding', 'Location', 'isDefault', 'index'],
}
]);
}
/**
* @desc Get the preference whether it wants a signed assertion response
* @return {boolean} Wantassertionssigned
*/
public isWantAssertionsSigned(): boolean {
return this.meta.spSSODescriptor.wantAssertionsSigned === 'true';
}
/**
* @desc Get the preference whether it signs request
* @return {boolean} Authnrequestssigned
*/
public isAuthnRequestSigned(): boolean {
return this.meta.spSSODescriptor.authnRequestsSigned === 'true';
}
/**
* @desc Get the entity endpoint for assertion consumer service
* @param {string} binding protocol binding (e.g. redirect, post)
* @return {string/[string]} URL of endpoint(s)
*/
public getAssertionConsumerService(binding: string): string | string[] {
if (isString(binding)) {
let location;
const bindName = namespace.binding[binding];
if (isNonEmptyArray(this.meta.assertionConsumerService)) {
this.meta.assertionConsumerService.forEach(obj => {
if (obj.binding === bindName) {
location = obj.location;
return;
}
});
} else {
if (this.meta.assertionConsumerService.binding === bindName) {
location = this.meta.assertionConsumerService.location;
}
}
return location;
}
return this.meta.assertionConsumerService;
}
}