1 | const fs = require( 'fs' );
|
2 | const config = require( './config' );
|
3 |
|
4 | const samlIssuer = config.passport.saml.issuer;
|
5 |
|
6 | const filePre = '\n'
|
7 |
|
8 | + '\n'
|
9 | + 'const fetchPromise = ( route, options ) => new Promise( ( resolve, reject ) => {\n'
|
10 | + '\n'
|
11 | + '\ttry {\n'
|
12 | + '\t\tfetch( route, options ) //eslint-disable-line\n'
|
13 | + '\t\t\t.then( resolve )\n'
|
14 | + '\t\t\t.catch( reject );\n'
|
15 | + '\t}\n'
|
16 | + '\n'
|
17 | + '\tcatch ( e ) {\n'
|
18 | + '\t\treject( e );\n'
|
19 | + '\t}\n'
|
20 | + '\n'
|
21 | + '} );\n'
|
22 | + '\n'
|
23 | + '\n'
|
24 | + 'const getUser = () => {\n'
|
25 | + '\n'
|
26 | + '\treturn new Promise ( ( res, rej ) => {'
|
27 | + '\t\tfetchPromise(';
|
28 |
|
29 | const filePost = '\', {\n'
|
30 | + '\t\t\theaders : {\n'
|
31 | + '\t\t\t\t\'Content-Type\' : \'application/json\',\n'
|
32 | + '\t\t\t},\n'
|
33 | + '\t\t\tmethod : \'GET\',\n'
|
34 | + '\t\t\tcredentials : \'include\'\n'
|
35 | + '\t\t}\n'
|
36 | + '\t)\n'
|
37 | + '\t\t.then( ( response ) => {\n'
|
38 | + '\t\t\tresponse.json().then( ( json ) => {\n'
|
39 | + '\n'
|
40 | + '\t\t\t\tif ( json.success && json.redirect ) {\n'
|
41 | + '\n'
|
42 | + '\t\t\t\t\twindow.location.href = json.redirectTo;\n'
|
43 | + '\n'
|
44 | + '\t\t\t\t} else {\n'
|
45 | + '\t\t\t\t\treturn json\n'
|
46 | + '\t\t\t\t}\n'
|
47 | + '\t\t\t} )\n'
|
48 | + '\t\t\t.then( ( user ) => {\n'
|
49 |
|
50 | + '\t\t\t\tconsole.log( user );\n'
|
51 | + '\t\t\t\tres ( user );\n'
|
52 | + '\t\t\t} );\n'
|
53 | + '\n'
|
54 | + '\t\t} );\n'
|
55 | + '\n\t} );'
|
56 | + '\n'
|
57 | + '};\n'
|
58 | + '\n';
|
59 |
|
60 |
|
61 | const contents = `${filePre}'${samlIssuer}/saml/get-profile${filePost}`;
|
62 |
|
63 | const fileInit = () => {
|
64 | fs.writeFile( `${__dirname}/../public/js/musd-saml.js`, contents, ( err ) => {
|
65 | if ( err ) throw err;
|
66 | } );
|
67 | };
|
68 |
|
69 | module.exports = {
|
70 | fileInit
|
71 | };
|