-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-cognito-identity.js
163 lines (140 loc) · 4.36 KB
/
aws-cognito-identity.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
angular.module('aws.cognito.identity', [
'aws',
'google.auth',
'facebook.auth'
]).provider('CognitoIdentity', function () {
var options = {
accountId: null,
poolId: null,
anonymousRole: null,
authenticatedRole: null
};
var providers = {};
var provider = {
config: function (extra) {
if (extra) {
options = angular.extend(options, extra);
return provider;
} else {
return options;
}
},
$get: function (
$q,
$injector,
$window,
$timeout,
$rootScope,
AWS
) {
var deferred = $q.defer();
var providersReference = 'aws.cognito.identity-providers';
// AWS credentials
var arnPrefix = 'arn:aws:iam::' + options.accountId + ':role/';
var baseCredentials = {
AccountId: options.accountId,
IdentityPoolId: options.poolId
};
var authenticatedCredentials = angular.extend({
RoleArn: arnPrefix + options.authenticatedRole
}, baseCredentials);
var service = {
promise: deferred.promise,
providers: providers,
isAuthenticated: null,
authenticate: function authenticateByProvider (providerId) {
return providers[providerId].authenticate();
},
getToken: getToken,
identify: function identify (providerId) {
if (
localStorage.getItem(providersReference) ||
localStorage.getItem(providersReference + '.' + options.poolId)
) {
service.authenticate(providerId);
} else if (options.anonymousRole) {
var anonymousCredentials = angular.extend({
RoleArn: arnPrefix + options.anonymousRole
}, baseCredentials);
AWS.config.credentials =
new AWS.CognitoIdentityCredentials(anonymousCredentials);
AWS.config.credentials.get(refreshCredentialsCallback);
}
}
};
function getToken (providerId, token) {
var params = authenticatedCredentials;
params.Logins = {};
params.Logins[providerId] = token;
AWS.config.credentials =
new AWS.CognitoIdentityCredentials(params);
AWS.config.credentials.refresh(function (error) {
refreshCredentialsCallback(error, provider);
});
return deferred.promise;
}
function refreshCredentialsCallback (error, provider) {
if (error) {
service.isIdentified = false;
$rootScope.$broadcast('aws.cognito.identity:identifyError', error);
deferred.reject(error);
} else {
var identityId = AWS.config.credentials.identityId;
var userArn = AWS.config.credentials.params.RoleArn;
var isAuthenticated =
(userArn === arnPrefix + options.authenticatedRole);
$rootScope.$apply(function () {
service.isAuthenticated = isAuthenticated;
});
var identityData = {
provider: provider,
identityId: identityId,
isAuthenticated: isAuthenticated
};
$rootScope.$broadcast('aws.cognito.identity:identifySuccess', identityData);
deferred.resolve(identityData);
}
return deferred.promise;
}
return service;
}
};
return provider;
}).factory('CognitoIdentityGoogle', function (
$timeout,
GoogleAuth,
CognitoIdentity
) {
var providerId = 'accounts.google.com';
var service = {
authenticate: function () {
return GoogleAuth.signIn().then(function (response) {
$timeout(function () {
service.authenticate();
}, parseInt(response.expires_in) * 1000);
return CognitoIdentity.getToken(providerId, response.id_token);
});
}
};
CognitoIdentity.providers[providerId] = service;
return service;
}).factory('CognitoIdentityFacebook', function (
$timeout,
FacebookAuth,
CognitoIdentity
) {
var providerId = 'graph.facebook.com';
var service = {
authenticate: function () {
return FacebookAuth.login().then(function (response) {
var res = response.authResponse;
$timeout(function () {
service.authenticate();
}, res.expiresIn * 1000);
return CognitoIdentity.getToken(providerId, res.accessToken);
});
}
};
CognitoIdentity.providers[providerId] = service;
return service;
});