forked from laardee/serverless-authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
202 lines (170 loc) · 6.67 KB
/
Copy pathutils.js
File metadata and controls
202 lines (170 loc) · 6.67 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
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Utils = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _jsonwebtoken = require('jsonwebtoken');
var _jsonwebtoken2 = _interopRequireDefault(_jsonwebtoken);
var _decamelize = require('decamelize');
var _decamelize2 = _interopRequireDefault(_decamelize);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Utilities for Serverless Authentication
*/
var Utils = exports.Utils = function () {
function Utils() {
_classCallCheck(this, Utils);
}
_createClass(Utils, null, [{
key: 'redirectUrlBuilder',
/**
* Creates redirectUrl
* @param url {string} url base
* @param provider {string} provider e.g. facebook
*/
value: function redirectUrlBuilder(url, provider) {
return url.replace(/{provider}/g, provider);
}
/**
* Creates url with params
* @param url {string} url base
* @param params {object} url params
*/
}, {
key: 'urlBuilder',
value: function urlBuilder(url, params) {
return url + '?' + this.urlParams(params);
}
/**
* Creates & separated params string
* @param params {object}
*/
}, {
key: 'urlParams',
value: function urlParams(params) {
var result = [];
for (var key in params) {
if (params.hasOwnProperty(key)) {
result.push((0, _decamelize2.default)(key) + '=' + params[key]);
}
}
return result.join('&');
}
/**
* Creates Json Web Token with data
* @param data {object}
* @param config {object} with token_secret --> change to secret
*/
}, {
key: 'createToken',
value: function createToken(data, secret, options) {
return _jsonwebtoken2.default.sign(data, secret, options);
}
/**
* Reads Json Web Token and returns object
* @param token {string}
* @param config {object} with token_secret --> change to secret
*/
}, {
key: 'readToken',
value: function readToken(token, secret, options) {
return _jsonwebtoken2.default.verify(token, secret, options);
}
/**
* Creates token response and triggers callback
* @param data {payload: object, options: object}
* @param config {redirect_client_uri {string}, token_secret {string}}
* @param callback {function} callback function e.g. context.done
*/
}, {
key: 'tokenResponse',
value: function tokenResponse(data, _ref, callback) {
var redirect_client_uri = _ref.redirect_client_uri,
redirect_method = _ref.redirect_method,
token_secret = _ref.token_secret;
var _data$authorizationTo = data.authorizationToken,
payload = _data$authorizationTo.payload,
options = _data$authorizationTo.options;
var params = {
authorizationToken: this.createToken(payload, token_secret, options)
};
for (var key in data) {
if (data.hasOwnProperty(key)) {
if (key !== 'authorizationToken') {
params[key] = data[key];
}
}
}
var result = {
url: redirect_client_uri,
method: redirect_method,
form: this.getRedirectForm(redirect_client_uri, redirect_method, params)
};
if (redirect_method !== 'POST') {
//Leave Default Behavior
result.url = this.urlBuilder(result.url, params);
}
return callback(null, result);
}
/**
* getRedirection Form - Takes a given target, HTTP Method, and params and creates a form that will auto-submit on page load.
* @param action - The location where the form should be submitted.
* @param method - The HTTP Method to use for the submission.
* @param params - An object of name/values to set the name/values of a hidden for for.
* @returns {string} - The HTML of a webpage which will submit the params to the action using the method on page load.
*/
}, {
key: 'getRedirectForm',
value: function getRedirectForm(action, method, params) {
var html = "<!DOCTYPE html><head><meta charset='UTF-8'><title>Redirecting...</title></head><body onload='document.redirectForm.submit()'>";
html += "<form name='redirectForm' action='" + action.replace("'") + "' method='" + method.replace("'") + "' >";
for (var name in params) {
if (!params.hasOwnProperty(name)) continue;
html += "<input type='hidden' name='" + name.replace("'") + "' value='" + params[name].replace("'") + "'>";
}
html += "</form></body></html>";
return html;
}
/**
* Creates error response and triggers callback
* @param params
* @param config {redirect_client_uri {string}}
* @param callback {function} callback function e.g. context.done
*/
}, {
key: 'errorResponse',
value: function errorResponse(params, _ref2, callback) {
var redirect_client_uri = _ref2.redirect_client_uri;
var url = this.urlBuilder(redirect_client_uri, params);
return callback(null, { url: url });
}
/**
* Generates Policy for AWS Api Gateway custom authorize
* @param principalId {string} data for principalId field
* @param effect {string} 'Allow' or 'Deny'
* @param resource {string} method arn e.g. event.methodArn
* (arn:aws:execute-api:<regionId>:<accountId>:<apiId>/<stage>/<method>/<resourcePath>)
*/
}, {
key: 'generatePolicy',
value: function generatePolicy(principalId, effect, resource) {
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
var policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
var statementOne = {};
statementOne.Action = 'execute-api:Invoke';
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
return authResponse;
}
}]);
return Utils;
}();