forked from tngan/samlify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata-idp.ts
152 lines (134 loc) Β· 4.33 KB
/
metadata-idp.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
/**
* @file metadata-idp.ts
* @author tngan
* @desc Metadata of identity provider
*/
import Metadata, { MetadataInterface } from './metadata';
import { MetadataIdpOptions, MetadataIdpConstructor } from './types';
import { namespace } from './urn';
import libsaml from './libsaml';
import { isNonEmptyArray, isString } from './utility';
import * as xml from 'xml';
export interface IdpMetadataInterface extends MetadataInterface {
}
/*
* @desc interface function
*/
export default function(meta: MetadataIdpConstructor) {
return new IdpMetadata(meta);
}
export class IdpMetadata extends Metadata {
constructor(meta: MetadataIdpConstructor) {
const isFile = isString(meta) || meta instanceof Buffer;
if (!isFile) {
const {
entityID,
signingCert,
encryptCert,
wantAuthnRequestsSigned = false,
nameIDFormat = [],
singleSignOnService = [],
singleLogoutService = [],
} = meta as MetadataIdpOptions;
const IDPSSODescriptor: any[] = [{
_attr: {
WantAuthnRequestsSigned: String(wantAuthnRequestsSigned),
protocolSupportEnumeration: namespace.names.protocol,
},
}];
if (signingCert) {
IDPSSODescriptor.push(libsaml.createKeySection('signing', signingCert));
} else {
//console.warn('Construct identity provider - missing signing certificate');
}
if (encryptCert) {
IDPSSODescriptor.push(libsaml.createKeySection('encryption', encryptCert));
} else {
//console.warn('Construct identity provider - missing encrypt certificate');
}
if (isNonEmptyArray(nameIDFormat)) {
nameIDFormat.forEach(f => IDPSSODescriptor.push({ NameIDFormat: f }));
}
if (isNonEmptyArray(singleSignOnService)) {
singleSignOnService.forEach((a, indexCount) => {
const attr: any = {
index: indexCount.toString(),
Binding: a.Binding,
Location: a.Location,
};
if (a.isDefault) {
attr.isDefault = true;
}
IDPSSODescriptor.push({ SingleSignOnService: [{ _attr: attr }] });
});
} else {
throw new Error('ERR_IDP_METADATA_MISSING_SINGLE_SIGN_ON_SERVICE');
}
if (isNonEmptyArray(singleLogoutService)) {
singleLogoutService.forEach((a, indexCount) => {
const attr: any = {};
if (a.isDefault) {
attr.isDefault = true;
}
attr.index = (indexCount).toString();
attr.Binding = a.Binding;
attr.Location = a.Location;
IDPSSODescriptor.push({ SingleLogoutService: [{ _attr: attr }] });
});
} else {
console.warn('Construct identity provider - missing endpoint of SingleLogoutService');
}
// Create a new metadata by setting
meta = xml([{
EntityDescriptor: [{
_attr: {
'xmlns': namespace.names.metadata,
'xmlns:assertion': namespace.names.assertion,
'xmlns:ds': 'http://www.w3.org/2000/09/xmldsig#',
entityID,
},
}, { IDPSSODescriptor }],
}]);
}
super(meta as string | Buffer, [
{
key: 'wantAuthnRequestsSigned',
localPath: ['EntityDescriptor', 'IDPSSODescriptor'],
attributes: ['WantAuthnRequestsSigned'],
},
{
key: 'singleSignOnService',
localPath: ['EntityDescriptor', 'IDPSSODescriptor', 'SingleSignOnService'],
index: ['Binding'],
attributePath: [],
attributes: ['Location']
},
]);
}
/**
* @desc Get the preference whether it wants a signed request
* @return {boolean} WantAuthnRequestsSigned
*/
isWantAuthnRequestsSigned(): boolean {
const was = this.meta.wantAuthnRequestsSigned;
if (was === undefined) {
return false;
}
return String(was) === 'true';
}
/**
* @desc Get the entity endpoint for single sign on service
* @param {string} binding protocol binding (e.g. redirect, post)
* @return {string/object} location
*/
getSingleSignOnService(binding: string): string | object {
if (isString(binding)) {
const bindName = namespace.binding[binding];
const service = this.meta.singleSignOnService[bindName];
if (service) {
return service;
}
}
return this.meta.singleSignOnService;
}
}