forked from vegeta999/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenHandler.js
More file actions
60 lines (48 loc) · 1.79 KB
/
Copy pathtokenHandler.js
File metadata and controls
60 lines (48 loc) · 1.79 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
import { decodeToken } from 'blockstack';
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { Match, check } from 'meteor/check';
import { logger } from './logger';
// Handler extracts data from JSON and tokenised reponse.
// Reflects OAuth token service, with some slight modifications for Blockstack.
//
// Uses 'iss' (issuer) as unique key (decentralised ID) for user.
// The 'did' final portion of the blockstack decentralised ID, is displayed as
// your profile ID in the service. This isn't used yet, but could be useful
// to link accounts if identity providers other than btc address are added.
export const handleAccessToken = (loginRequest) => {
logger.debug('Login request received', loginRequest);
check(loginRequest, Match.ObjectIncluding({
authResponse: String,
userData: Object,
}));
// Decode auth response for user attributes
const { username, profile } = loginRequest.userData;
const decodedToken = decodeToken(loginRequest.authResponse).payload;
profile.username = username;
logger.debug('User data', loginRequest.userData);
logger.debug('Login decoded', decodedToken);
const { iss, iat, exp } = decodedToken;
if (!iss) {
return {
type: 'blockstack',
error: new Meteor.Error(Accounts.LoginCancelledError.numericError, 'Insufficient data in auth response token'),
};
}
// Collect basic auth provider details
const serviceData = {
id: iss,
did: iss.split(':').pop(),
issuedAt: new Date(iat * 1000),
expiresAt: new Date(exp * 1000),
};
// Add Avatar image source to use for auth service suggestions
if (Array.isArray(profile.image) && profile.image.length) {
serviceData.image = profile.image[0].contentUrl;
}
logger.debug('Login data', serviceData, profile);
return {
serviceData,
options: { profile },
};
};