-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartong-arcgis-token.js
68 lines (56 loc) · 1.91 KB
/
cartong-arcgis-token.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
//TODO: this plugin is not totally finalized. To embed in Arcgis.Service class.
var CartONG = window.CartONG || {};
CartONG.ArcgisToken = (function () {
function ArcgisToken(response) {
if (response.token) {
this.token = response.token;
this.expires = response.expires; //in miliseconds
this.requestTime = response.requestTime ? response.requestTime : new Date().getTime(); //in miliseconds
}
else if (response.error) {
this.error = {};
this.error.code = response.error.code;
this.error.message = response.error.message;
this.error.details = response.error.details;
}
else {
this.error.message = 'ArcGIS Token unknown error';
}
}
ArcgisToken.prototype.isSuccess = function () {
return this.token ? true : false;
}
ArcgisToken.prototype.isExpired = function () {
const now = new Date().getTime()
return this.expires < now;
}
ArcgisToken.prototype.getToken = function () {
return this.token ? this.token : false;
}
ArcgisToken.prototype.getExpire = function () {
return this.expires ? this.expires : false;
}
ArcgisToken.prototype.getErrorMessage = function () {
return this.error.message ? this.error.message : false;
}
//ArcgisToken.prototype.request = function (username, password, tokenService, expiration) {
ArcgisToken.request = function (username, password, tokenService, expiration) {
expiration = expiration || 20 //20 minutes by default if expiration is not expressly provided
// function to generate a token
var serverAuth = $.post(tokenService, {
username: username,
password: password,
f: 'json',
expiration: expiration, // given in minutes - esri says
client: 'requestip',
});
// function to wait for token
return $.when(serverAuth).then(function(response, status){
return new ArcgisToken(JSON.parse(response));
});
};
ArcgisToken.fromJSON = function (json) {
return new ArcgisToken(json);
};
return ArcgisToken;
}());