-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlambda.js
executable file
·164 lines (150 loc) · 4.32 KB
/
lambda.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
require('dotenv').load();
var jwt = require('jwt-simple');
var moment = require('moment');
var CognitoHelper = require('./cognito-helper');
var cognito = new CognitoHelper();
var config = require('./server-config');
console.log('lambda loaded config', config);
/*
|--------------------------------------------------------------------------
| Generate JSON Web Token
|--------------------------------------------------------------------------
*/
function createJWT(userId, expiresIn) {
var exp;
if(config.EXPIRES_IN) {
exp = moment().add(config.EXPIRES_IN, 'seconds');
}
else if(expiresIn) {
exp = moment().add(expiresIn, 'seconds');
}
else {
exp = moment().add(14, 'days');
}
console.log('createJWT exp', exp.format());
var payload = {
sub: userId,
iat: moment().unix(),
exp: exp.unix(),
};
console.log('createJWT payload', payload);
return jwt.encode(payload, config.TOKEN_SECRET);
}
/*
|--------------------------------------------------------------------------
| Verify JWT token for authenticated requests
|--------------------------------------------------------------------------
*/
function checkJWT(authorization, dontFail) {
if (!authorization) {
if(dontFail) {
return null;
}
else {
return {code: 401, message: 'Missing Authorization header'};
}
}
var token = authorization.split(' ')[1];
var payload = jwt.decode(token, config.TOKEN_SECRET);
console.log('checkJWT', payload);
var now = moment().unix();
console.log('checkJWT', 'exp=' + payload.exp + ' now=' + now);
if (payload.exp <= now - 60) {
if(dontFail) {
return null;
}
else {
return {code: 401, message: 'Token has expired'};
}
}
return payload.sub;
}
/*
|--------------------------------------------------------------------------
| AWS invokes this method to process requests
|--------------------------------------------------------------------------
*/
exports.handler = function(event, context) {
console.log('event', event);
// /auth/{operation}
var operation = event.operation;
var payload = event.payload;
var ensureAuthenticated = function(callback) {
var authorization = event.authorization;
delete event.authorization;
var t = checkJWT(authorization);
if(t.message) {
context.fail(new Error('Unauthorized: ' + t.message));
}
else {
callback(t);
}
};
var dataCallback = function(err, data) {
if(err) {
context.fail(makeError(err));
}
else {
context.succeed(data);
}
};
var makeError = function(err) {
var errorCode = 'Bad Request';
switch(err.code) {
case 404: errorCode = 'Not Found'; break;
case 409: errorCode = 'Conflict'; break;
case 401: errorCode = 'Unauthorized'; break;
}
return new Error(errorCode + ': ' + (err.error || err));
};
var tokenCallback = function(err, data) {
// console.log('tokenCallback err', err);
// console.log('tokenCallback data', data);
if(err) {
context.fail(makeError(err));
}
else {
context.succeed({token: createJWT(data.id)});
}
};
if(operation === 'login') {
cognito.login(payload.email, payload.password, payload.reset,
tokenCallback);
}
else if(operation === 'signup') {
cognito.signup(payload.name, payload.email, payload.password,
tokenCallback);
}
else if(operation === 'me') {
ensureAuthenticated(function(userId) {
cognito.getProfile(userId, dataCallback);
});
}
else if(operation === 'credentials') {
ensureAuthenticated(function(userId) {
cognito.getCredentials(userId, dataCallback);
});
}
else if(operation === 'forgot') {
cognito.forgotPassword(payload.email, dataCallback);
}
else if(operation === 'update') {
ensureAuthenticated(function(userId) {
cognito.updatePassword(userId, payload.password, dataCallback);
});
}
else if(operation === 'unlink') {
ensureAuthenticated(function(userId) {
cognito.unlink(userId, payload.provider, null, dataCallback);
});
}
else {
var provider = operation;
var userId = checkJWT(event.authorization, true);
console.log('provider', provider);
console.log('userId', userId);
cognito.loginFederated(provider,
payload.code, payload.clientId, payload.redirectUri, userId,
tokenCallback);
}
};