forked from ForgeRock/openamjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenam.js
1220 lines (1134 loc) · 43.6 KB
/
openam.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2016 ForgeRock, AS.
*
* This is unsupported code made available by ForgeRock for community development
* subject to the license detailed below. The code is provided on an "as is" basis,
* without warranty of any kind, to the fullest extent permitted by law.
*
* ForgeRock does not warrant or guarantee the individual success developers may
* have in implementing the code on their development platforms or in production
* configurations.
*
* ForgeRock does not warrant, guarantee or make any representations regarding the
* use, results of use, accuracy, timeliness or completeness of any data or
* information relating to the alpha release of unsupported code. ForgeRock
* disclaims all warranties, expressed or implied, and in particular, disclaims all
* warranties of merchantability, and warranties related to the code, or any
* service or software related thereto.
*
* ForgeRock shall not be liable for any direct, indirect or consequential damages
* or costs of any type arising out of any action taken by you or others related to
* the code.
*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at https://forgerock.org/cddlv1-0/. See the
* License for the specific language governing permission and limitations under the
* License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file
* and include the License file at https://forgerock.org/cddlv1-0/. If applicable,
* add the following below the CDDL Header, with the fields enclosed by brackets []
* replaced by your own identifying information: "Portions copyright [year] [name
* of copyright owner]".
*/
/* Some useful wrappers around the OpenAM APIs
* To be extended further
* openam.js (v0.1)
* Author: Identity Wrestler
* Notice: This wrapper is not supported/maintained/endorsed by ForgeRock
*/
// TO DO: The Legacy Functions are not implemented yet.
// Some global variables
var debugEnabled = false;
var storageExist = ("sessionStorage" in window && window.sessionStorage);
/*
* Opens a window of height h and displays the URL url
* @param {type} url The URL that the window will display
* @param {type} h The height of the window
*/
var openWindow = function (url, h) {
var left = (screen.width / 2) - (900 / 2);
var top = (screen.height / 2) - (600 / 2);
var height = (h.height !== null) ? h.height : 520;
var win = window.open(url, "win", "width=900, height=" + height + ", top=" + top + ", left=" + left);
win.focus();
var id = setInterval(function () {
try {
var loc = win.location.href;
if (win.location.href.indexOf(url) < 0) {
clearInterval(id);
win.opener.location.replace(loc);
win.close();
}
} catch (ex1) {
// Do nothing
};
}, 500);
};
/**
* Gets the path/dir of the page running the script
* @function getMyURLDir
* @returns {String}
*/
function getMyURLDir() {
var loc = window.location.pathname;
return window.location.protocol + "//" + window.location.hostname + ":" +
window.location.port +
loc.substring(0, loc.lastIndexOf('/') + 1);
}
/**
* Gets the URL of the page running the script
* @function getMyURL
* @returns {String}
*/
function getMyURL() {
var host = window.location.hostname;
var protocol = window.location.protocol;
var port = window.location.port;
var path = window.location.pathname;
return protocol + "//" + host + ":" + port + path;
}
/**
* Create a Cookie for the domain specified in domainName
* @function createCookie
* @param {type} name Name of the cookie to be created
* @param {type} value Value for the cookie
* @param {type} hours Time that the cookie will exist
* @param {type} domainName Domain in which the cookie will be created
*/
function createCookie(name, value, hours, domainName) {
var expires;
var domain;
if (hours) {
var date = new Date();
date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
domain = ";domain=" + domainName;
} else {
expires = "";
domain = ";domain=" + domainName;
}
document.cookie = escape(name) + "=" + escape(value) + expires + domain + "; path=/";
}
/**
* Deletes the coookie
* @deleteCookie
* @param {type} name Name of the cookie to be deleted
* @param {type} domainName Domain where the cookie resides
*/
function deleteCookie(name, domainName) {
createCookie(name, "", -1, domainName);
}
/**
* Get's the value of the cookie specified
* @function getCookie
* @param {type} name The name of the coookie whose value we want to retrieve
* @returns {String} The value of the cookie
*/
function getCookie(name) {
var value = " " + document.cookie;
var cStart = value.indexOf(" " + name + "=");
if (cStart === -1) {
value = null;
}
else {
cStart = value.indexOf("=", cStart) + 1;
var cEnd = value.indexOf(";", cStart);
if (cEnd === -1) {
cEnd = value.length;
}
value = unescape(value.substring(cStart,cEnd));
}
return value;
}
/**
* Gets the value stored in the Local session store. Using the key specified by
* @function getLocal
* @param {type} storageKey The key of the value to retrive
* @returns {type} data The value of the value retrieved
*/
function getLocal(storageKey) {
var now, expiration;
var storedItem = false;
var storedItemS = null;
var data = false;
try {
if (storageExist) {
storedItem = sessionStorage.getItem(storageKey);
debug("getLocal: There is sessionStorage");
if (storedItem) {
storedItemS = JSON.parse(storedItem);
debug("getLocal: There is Data stored: " + storedItemS);
now = new Date();
expiration = new Date(storedItemS.timestamp);
expiration.setMinutes(expiration.getMinutes() + this.cacheTime);
if (now.getTime() >= expiration.getTime()) {
debug("getLocal: DATA EXPIRED ");
data = false;
sessionStorage.removeItem(storageKey);
} else {
debug("getLocal: DATA NOT EXPIRED ");
data = storedItemS.data;
data = data.toLocaleString();
debug("getLocal: Data parsed: " + data);
}
}
}
} catch (error) {
debug("getLocal: STORAGE ERROR" + error);
data =false;
}
return data;
};
/**
* Stores a value "data" in the key "storageKey" in the local session storage
* @function storeLocal
* @param {type} storageKey - The key to be used to store the value
* @param {type} data - The value of the data to be stored
*/
function storeLocal(storageKey, data) {
if (storageExist) {
try {
debug("storeLocal: DATA TO STORE IS: " + storageKey + ":" + data);
sessionStorage.setItem(storageKey,
JSON.stringify({
timestamp: new Date(),
data: data}));
} catch (err) {
// Nothing
}
}
};
/*
* Removes the value for the storageKey
* @function removeLocal
* @param {type} storageKey
* @returns {undefined}
*/
function removeLocal(storageKey) {
if (storageExist) {
try {
debug("removeLocal: REMOVING " + storageKey);
sessionStorage.removeItem(storageKey);
} catch (err) {
// Do nothing
}
}
};
/**
* Removes the whole local session storage
* @function removeAlllocal
* @returns {undefined}
*/
function removeAllLocal() {
if (storageExist) {
try {
debug("removeAllLocal: REMOVING ALL");
removeLocal("validSession");
removeLocal("attributes");
} catch (err) {
// Do nothing
}
}
};
/**
* Displays a message in the browser's console (if possible)
* @function debug
* @param {type} message - Message to display in the console
* @returns {undefined}
*/
function debug(message) {
if (debugEnabled) {
try {
console.log(message);
} catch (err) {
alert("Debugging will be disabled, your browser does not support it");
debugEnabled = false;
}
}
};
// Functions to connect to the server using XMLHttpRequest
// or MSoft stuff, in case is necessary
var ajax = {};
ajax.x = function () {
if ('withCredentials' in new XMLHttpRequest()) {
debug("ajax: XHR support");
return new XMLHttpRequest();
} else if (typeof XDomainRequest !== "undefined") {
debug("ajax: XDR support");
return new XDomainRequest();
} else {
var versions = [
"MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"
];
var xhr;
for (var i = 0; i < versions.length; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
} catch (e) {
debug("ajax: ERROR when creating ActiveXObject");
}
}
debug("ajax: ActiveXObject support");
return xhr;
}
};
ajax.send = function(url, callback, method, data, contentType, sessionCookieName, tokenId, sync) {
var x = ajax.x();
x.open(method, url, sync, null, null);
x.onreadystatechange = function() {
if (x.readyState === 4) {
callback(x.responseText);
}
};
x.setRequestHeader('Content-type', contentType);
if (tokenId && sessionCookieName) {
x.setRequestHeader(sessionCookieName, tokenId);
}
x.send(data);
};
ajax.authenticate = function (url, callback, data, headers, sync) {
var x = ajax.x();
x.onreadystatechange = function () {
if (x.readyState === 4) {
callback(x.responseText, x.getAllResponseHeaders());
}
};
try {
x.open('POST', url, false);
} catch (err) {
debug("ERROR" + err);
}
if (headers) {
for (var option in headers) {
try {
x.setRequestHeader(option, headers[option]);
} catch (err) {
debug("ERROR" + err);
}
}
}
x.setRequestHeader('Content-type', 'application/json');
x.send(data);
};
ajax.get = function(url, callback, contentType, sessionCookieName, tokenId, sync) {
ajax.send(url, callback, 'GET', null, contentType, sessionCookieName, tokenId, sync);
};
ajax.post = function(url, data, callback, contentType, sync) {
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
ajax.send(url, callback, 'POST', query.join('&'), contentType, sync);
};
/**
* OpenAM Configuration instance
* @function openamConfig
* @param {Object} options - The OpenAM Configuration JSON object.
* <pre>
* Here an example.
* {
* baseurl: "http://openam1.example.com:8080/openam",
* realm: "/", // optional
* cachetime: 3, // optional
* debugenabled: true // optional
* }
* </pre>
* @param {String} options.baseurl - The URL where OpenAM is running, example:
* "https://openam.example.com:443/openam"
* @param {String} [options.realm=The default realm for the baseurl used] - Name
* of the realm to be used, example: "/"
* @param {String} [options.cachetime=3] - Time in minutes the session valid response
* and attributes are cached in the session store (if possible). To disable caching
* set the time to 0. Example of caching for 3 minutes: 3
* @param {String} [options.debugenabled=false] - Enable debug, works for some browser,
* not for all. Example: true
* @returns {openamConfig} An instance of the OpenAM Configuration.
*/
function openamConfig(options) {
if (options === undefined) {
throw "Object configuration must be specified";
}
if (Boolean(options.debugenabled)) {
debugEnabled = true;
}
// Some important service URIs
this.XUILoginURI = function() {
return "/XUI/#login";
};
this.XUILogoutURI = function() {
return "/XUI/#logout";
};
this.LoginURI = function() {
return "/UI/Login";
};
this.LogoutURI = function() {
return "/UI/Logout";
};
this.ProfileURI = function() {
return "/XUI/#profile/";
};
// OpenAM REST API URIs
this.sessionsURI = function() {
return "/json/sessions";
};
this.authenticationURI = function() {
return "/json/authenticate";
};
this.attributesURI = function() {
return "/json/users/";
};
this.serverinfoURI = function() {
return "/json/serverinfo/*";
};
// OpenAM Legacy REST API URIs
this.legacyAuthenticationURI = function() {
return "/identity/json/authenticate";
};
this.legacyAttributesURI = function() {
return "/identity/json/attributes";
};
this.legacyValidateSession = function() {
return "/identity/json/isTokenValid";
};
this.legacyLogoutURI = function() {
return "/identity/logout";
};
// OpenAM Param names
this.logoutURI = function() {
return "logout";
};
this.realmParam = function() {
return "realm";
};
this.serviceParam = function() {
return "service";
};
this.moduleParam = function() {
return "module";
};
this.authTypeParam = function() {
return "authIndexType";
};
this.authValueParam = function() {
return "authIndexValue";
};
// The Unique Identifier defined in OpenAM for the Identity, ususallly uid
this.uniqueId = 'uid';
// The value for the attribute defined by uniqueID. Initially empty.
this.Id = '';
debug("openamConfig: Unique ID for this configuration is: " + this.uniqueId);
this.openAMBaseURL = function () {
return options.baseurl;
};
this.realm = function () {
if (options.realm) {
return options.realm;
} else {
return "/";
}
};
serverinfo_url = options.baseurl + this.serverinfoURI();
if (options.realm && options.realm !== '' && options.realm !== '/') {
serverinfo_url = serverinfo_url.replace("/json", "/json" + options.realm);
}
function serverinfoURL() {
return serverinfo_url;
};
var response_sir = null;
var serverinfo_url = serverinfoURL();
debug("openamConfig: Serverinfo URL: " + serverinfo_url);
ajax.get(serverinfo_url,
function (responseText) {
response_sir = responseText;
},
'application/json');
function serverinfo() {
return response_sir;
};
var cookieName = JSON.parse(serverinfo()).cookieName;
debug("openamConfig: Session Cookie Name=" + cookieName);
this.sessionCookieName = function () {
return cookieName;
};
// Taking the first domain
// ToDo: Use all the domains and restrict as an option
var domain = JSON.parse(response_sir).domains[0];
this.domainName = function () {
return domain;
};
// Legacy not implemented yet
this.legacyEnabled = false;
var ssotoken_ = getCookie(cookieName);
this.ssotoken = function () {
return ssotoken_;
};
this.XUILoginURL = function () {
return options.baseurl + this.XUILoginURI();
};
this.loginURL = function () {
return options.baseurl + this.LoginURI();
};
this.cacheTime = 0;
this.cacheEnabled = false;
if (storageExist && options.cachetime > 0) {
this.cacheTime = options.cachetime || 3;
this.cacheEnabled = true;
}
this.socialImplementations = function () {
return JSON.parse(response_sir).socialImplementations;
};
/*
* Provides the URL to be used for the authentication based on the module or service provided
* @param {String | undefined} module - AuthN module to be used. Default is the default AuthN module configured in the OpenAM for the realm
* @param {String | undefined} service - Name of the service chain to be used for the authentication
* @returns {string} - Returns the URL of the authentication endpoint for the realm and module or service provided
*/
this.authenticationURL = function (module, service, orealm) {
debug("openamConfig.authenticationURL:OpenAM Base URL: " + options.baseurl);
var realm_ = orealm || options.realm;
var authentication_url = options.baseurl + this.authenticationURI() +
"?" + this.realmParam() + "=" + realm_;
if (module && module !== '') {
if (authentication_url.indexOf("?") !== -1) {
authentication_url = authentication_url + "&" + this.authTypeParam() + "=" + this.moduleParam() + "&" +
this.authValueParam() + "=" + module;
} else {
authentication_url = authentication_url + "?" + this.authTypeParam() + "=" + this.moduleParam() + "&" +
this.authValueParam() + "=" + module;
}
} else {
if (service && service !== '') {
if (authentication_url.indexOf("?") !== -1) {
authentication_url = authentication_url + "&" + this.authTypeParam() + "=" + this.serviceParam() + "&" +
this.authValueParam() + "=" + service;
} else {
authentication_url = authentication_url + "?" + this.authTypeParam() + "=" + this.serviceParam() + "&" +
this.authValueParam() + "=" + service;
}
}
}
return authentication_url;
};
/*
* Provides the proper OpenAM Attributes URL using the configured realm
* @param {String} orealm The realm to be used to build the URL
* @returns {String} - Returns the URL of the attributes end point of the OpenAM
*/
this.attributesURL = function (orealm) {
var attributes_url = options.baseurl + this.attributesURI();
if (options.realm !== '' & options.realm !== '/') {
var realm_op = options.realm;
}
realm_ = orealm || realm_op;
if (realm_ !== '' & realm_ !== '/') {
realm_ = orealm || options.realm;
attributes_url = attributes_url.replace("/users", realm_ + "/users");
}
return attributes_url;
};
/*
* Provides the OpenAM authentication URL using the parameters configured
* @param {String} tokenId - The token id that represents the OpenAM session
* @returns {String} - Returns The URL of the logout endpoint for a modern OpenAM
*/
this.logoutURL = function (tokenId) {
if (!this.legacyEnabled) {
var logout_url = options.baseurl + this.sessionsURI() + "/" + tokenId + "?_action=logout";
}
return logout_url;
};
/*
* Provides the OpenAM sessions URL using the parameters configured
* @returns {String} - Returns The URL of the sessions endpoint for a modern OpenAM
*/
this.sessionsURL = function () {
if (!this.legacyEnabled) {
return options.baseurl + this.sessionsURI();
} else {
return options.baseurl + this.legacyValidateSession();
}
};
}
/**
* Redirects for authentication to an OpenAM using the Authentication module specified
* @function authNRedirect
* @param {Object} options - The configuration object to use
* <pre>
* The options object is a JSON object, here an example.
* {
* openam: myOpenAMConfigObject,
* module: "DataStore", // optional
* service: "ldapService", // optional
* gotoURL: "http://ap.example.com:8880/exampleNRO02.html", // optional
* gotoOnFail: "http://ap.example.com:8880/exampleNRO02.html", // optional
* classic: false, // optional
* windowed: true // optional
* }
* </pre>
* @param {Object|String} options.openam The OpenAM Configuration Object. This is
* a mandatory attribute. See openam.js documentation for more information
* @param {String} [options.module=OpenAM realm default] The Authentication module
* to use in the left side of the login box.
* @param {String} [options.service=OpenAM realm default] The Authentication service
* chain to use in the left side of the login box. Notice that service takes
* precedence over module.
* @param {String} [options.gotoURL=Current page] The URL to go to after a
* successful authentication.
* @param {String} [options.gotoOnFail=Current page] The URL to go to after an
* authentication event has failed.
* @param {String} [options.classic=false] Boolean attribute to specify if we are
* using the classic UI (true) or the XUI (false). Default is to use the XUI.
* @param {String} [options.windowed=true] Boolean attribute to specify if the
* redirect will happen in a pop-up window or not.
*/
openamConfig.prototype.authNRedirect = function (options)
{
var redirectURL = "";
var myURL = encodeURIComponent(getMyURL());
if (options) {
var module = options.module || null;
var service = options.service || null;
var windowed = true;
if (options.windowed !== undefined) {
windowed = options.windowed;
}
var gotoURL = options.gotoURL || myURL;
var gotoOnFail = options.gotoOnFail || myURL;
var classic = options.classic || false;
var realm_ = options.realm || this.realm();
}
if (!module || module === '') {
module = '';
debug("authNRedirect: module present:" + module);
}
if (!service || service === '') {
service = '';
debug("authNRedirect: service present:" + service);
}
if (module) {
if (!classic) {
redirectURL = this.XUILoginURL() + realm_ + "&" + this.moduleParam() + "=" +
module + "&goto=" + gotoURL + "&gotoOnFail=" + gotoOnFail;
} else {
redirectURL = this.loginURL() + "?" + this.realmParam() + "=" + realm_ + "&" +
this.moduleParam() + "=" + module + "&goto=" + gotoURL +
"&gotoOnFail=" + gotoOnFail;
}
}
if (service) {
if (!classic) {
redirectURL = this.XUILoginURL() + realm_ + "&" + this.serviceParam() + "=" +
service + "&goto=" + gotoURL + "&gotoOnFail=" + gotoOnFail;
} else {
redirectURL = this.loginURL() + "?" + this.realmParam() + "=" + realm_ + "&" +
this.serviceParam() + "=" + service + "&goto=" + gotoURL +
"&gotoOnFail=" + gotoOnFail;
}
}
if (!module && !service) {
if (!classic) {
redirectURL = this.XUILoginURL() + realm_ +
"&goto=" + gotoURL + "&gotoOnFail=" + gotoOnFail;
} else {
redirectURL = this.loginURL() + "?" + this.realmParam() + "=" + realm_ +
"&goto=" + gotoURL + "&gotoOnFail=" + gotoOnFail;
}
}
if (windowed) {
openWindow(redirectURL, {height: 500});
} else {
window.location = redirectURL;
}
return false;
};
/**
* Checks if a user is authenticated
* @function isUserAuthenticated
* @returns {Boolean} - True if a user is authenticated
*/
openamConfig.prototype.isUserAuthenticated = function () {
if (this.ssotoken() && this.ssotoken() !== '' && this.isSessionValid(this.ssotoken())) {
debug("isUserAuthenticated: USER AUTHENTICATED");
return true;
} else {
debug("isUserAuthenticated: USER NOT AUTHENTICATED");
return false;
}
};
/**
* Checks if the session that the tokenID represents is valid
* @function isSessionValid
* @param {String} tokenId - The SSO Token ID (a.k.a the identifier of the session)
* @returns {Boolean} - True if the session is valid
*/
openamConfig.prototype.isSessionValid = function (tokenId) {
var valid = false;
var sessions_url = "";
var response = null;
response = getLocal("validSession");
debug("isSessionValid cached response: " + response);
if (response) {
var parsedR = JSON.parse(response);
valid = parsedR.valid;
this.Id = parsedR[this.uniqueId];
if (valid && this.id) {
return valid;
}
}
if (!this.legacyEnabled) {
debug("isSessionValid: Legacy Mode Disabled");
sessions_url = this.sessionsURL();
ajax.post(sessions_url + "/" + tokenId + "?_action=validate",
null,function(responseText) {
response = responseText;
},
'application/json');
var parsedR = JSON.parse(response);
valid = parsedR.valid;
this.Id = parsedR[this.uniqueId];
debug("VALID IS: " + valid);
} else {
debug("isSessionValid: Legacy Mode Enabled");
sessions_url = this.sessionsURL();
ajax.post(sessions_url + "?tokenid=" + tokenId, null,
function(responseText) {
valid = JSON.parse(responseText).valid;
},
'application/json');
}
storeLocal("validSession", response);
debug("isSessionValid: isValid Response: " + valid + "; " + this.uniqueId + "="
+ this.Id);
return valid;
};
/**
* Authenticates an identity using any authentication module
* The version of the AM should support the /json/authenticate endpoint.
* @function authenticate
* @param {Object} options - The configuration object to use
* <pre>
* The options object is a JSON object, here an example.
* {
* module: "DataStore", // optional
* service: "ldapService", // optional
* realm: "/", // optional
* headers: "http://ap.example.com:8880/exampleNRO02.html", // optional
* data: objectData // optional
* }
* </pre>
* @param {String} [options.module=OpenAM realm default] The Authentication module
* to use in the left side of the login box.
* @param {String} [options.service=OpenAM realm default] The Authentication service
* chain to use in the left side of the login box. Notice that service takes
* precedence over module.
* @param {String} [options.realm=The one configured in openam.js] Realm where the
* authentication will take place
* @param {Object} options.headers - Object containing the credentials passed as headers
* @param {Object} [options.data={}] - The payload to be submitted to the authentication
* module
*
*/
openamConfig.prototype.authenticate = function (options) {
if (!this.legacyEnabled) {
return this.authenticateWithModernOpenAM(options);
} else {
return this.authenticateWithLegacyOpenAM(options);
}
};
/*
* Authenticates an identity using any authentication module
* The version of the AM should support the /json/authenticate endpoint.
* authenticateWithModernOpenAM
* @param {Object} options - The configuration object to use
* <pre>
* The options object is a JSON object, here an example.
* {
* module: "DataStore", // optional
* service: "ldapService", // optional
* realm: "/", // optional
* headers: "http://ap.example.com:8880/exampleNRO02.html", // optional
* data: objectData // optional
* }
* </pre>
* @param {String} [options.module=OpenAM realm default] The Authentication module
* to use in the left side of the login box.
* @param {String} [options.service=OpenAM realm default] The Authentication service
* chain to use in the left side of the login box. Notice that service takes
* precedence over module.
* @param {String} [options.realm=The one configured in openam.js] Realm where the
* authentication will take place
* @param {Object} options.headers - Object containing the credentials passed as headers
* @param {Object} [options.data={}] - The payload to be submitted to the authentication
* module
*/
openamConfig.prototype.authenticateWithModernOpenAM = function(options) {
if (options) {
var module = options.module || null;
var service = options.service || null;
var headers = options.headers || null;
var data = options.data || "{}";
var realm = options.realm || this.realm();
}
if (!module || module === '') {
module = '';
}
if (!service || service === '') {
service = '';
}
var tokenId = null;
var response = null;
var allHeaders = null;
var authentication_url = this.authenticationURL(module, service, realm);
debug("authenticateWithModernOpenAM: AUTHN URL: " + authentication_url);
try {
ajax.authenticate(authentication_url,
function (responseText, allHeaders_) {
if (responseText) {
tokenId = JSON.parse(responseText).tokenId;
response = responseText;
allHeaders = allHeaders_;
}
}, data, headers);
} catch (err) {
debug("authenticateWithModernOpenAM: " + err);
}
if (tokenId && tokenId.length !== 0) {
createCookie(this.sessionCookieName(), tokenId, 3, this.domainName());
debug("authenticateWithModernOpenAM: RESPONSE: " + response);
// authentication.tokenid = tokenId;
} else {
debug("authenticateWithModernOpenAM: RESPONSE: " + response);
if (JSON.parse(response).authId) {
// there was an authId in the respone, there is more stuff coming
return response;
} else {
deleteCookie(this.sessionCookieName());
removeLocal("validSession");
}
}
debug("authenticateWithModernOpenAM.TOKENID: " + tokenId);
return tokenId;
};
/*
* Authenticates an identity using a one state authentication module.
* The version of the AM should support the /json/authenticate endpoint.
* The realm, module or service can be specified but only modules and services
* with one state and credentials passed in headers are supported at the moment
* @param {Object} options - The configuration object to use
* <pre>
* The options object is a JSON object, here an example.
* {
* module: "DataStore", // optional
* service: "ldapService", // optional
* headers: myHeaders,
* realm: "/", // optional
* gotoURL: "https://app.example.com:8080/mypath", // optional
* gotoOnFail: "https://app.example.com:8080/failed", // optional
* }
* </pre>
* @param {String} [options.module=OpenAM realm default] The Authentication module
* to use in the left side of the login box.
* @param {String} [options.service=OpenAM realm default] The Authentication service
* chain to use in the left side of the login box. Notice that service takes
* precedence over module.
* @param {String} [options.realm=The one configured in openam.js] Realm where the
* authentication will take place
* @param {Object} options.headers - Object containing the credentials passed as headers
* @param {String} [options.gotoURL=Current page] The URL to go to after a
* successful authentication.
* @param {String} [options.gotoOnFail=Current page] The URL to go to after an
* authentication event has failed.
*/
//openamConfig.prototype.authenticateSimple = function (options) {
// var gotoURL = options.gotoURL || getMyURL();
// var gotoOnFail = options.gotoOnFail || getMyURL();
// var tokenId = this.authenticateWithModernOpenAM(options);
// if (tokenId) {
// window.location = gotoURL;
// } else {
// if (gotoOnFail) {
// window.location = gotoOnFail;
// } else {
// throw("Authentication failed");
// }
// }
//};
/**
* Authenticates an identity using a one state authentication module by using
* the values submitted either in the form containing username and password or
* by using credentials submitted in the headers object.
* The version of the AM should support the /json/authenticate endpoint.
* The realm, module or service can be specified but only modules and services
* with one state are supported.
* @function authenticateSimple
* @param {Object} options - The configuration object to use
* <pre>
* The options object is a JSON object, here an example.
* {
* module: "DataStore", // optional
* service: "ldapService", // optional
* username: 'usernameField', // optional
* password: 'passwordField', // optional
* headers: myHeaders, // optional
* realm: "/", // optional
* gotoURL: "https://app.example.com:8080/mypath", // optional
* gotoOnFail: "https://app.example.com:8080/failed", // optional
* }
* </pre>
* @param {String} [options.module=OpenAM realm default] The Authentication module
* to use in the left side of the login box.
* @param {String} [options.service=OpenAM realm default] The Authentication service
* chain to use in the left side of the login box. Notice that service takes
* precedence over module.
* @param {String} [options.username='username'] The id of the field that
* contains the username in the form. Either username and password or headers
* must be specified.
* @param {String} [options.password='password'] The id of the field that
* contains the password in the form. Either username and password or headers
* must be specified.
* @param {Object} options.headers - Object containing the credentials passed
* as headers
* @param {String} [options.realm=The one configured in openam.js] Realm where the
* authentication will take place
* @param {Object} [options.headers] - Object containing the credentials passed
* as headers. Either username and password or headers must be specified.
* @param {String} [options.gotoURL=Current page] The URL to go to after a
* successful authentication.
* @param {String} [options.gotoOnFail=Current page] The URL to go to after an
* authentication event has failed.
*/
openamConfig.prototype.authenticateSimple = function (options) {
var gotoURL = options.gotoURL || getMyURL();
var gotoOnFail = options.gotoOnFail || getMyURL();
try {
var usernameField = options.username || "username";
var passwordField = options.password || "password";
var username = document.getElementById(usernameField).value;
var password = document.getElementById(passwordField).value;
} catch (err) {
// Do nothing
}
if (username && username !== '' && password && password !== '') {
options.headers = {
"X-OpenAM-Username": username,
"X-OpenAM-Password": password
};
}
var tokenId = this.authenticateWithModernOpenAM(options);
if (tokenId) {
window.location = gotoURL;
} else {
if (gotoOnFail) {