-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.js
102 lines (94 loc) · 2.68 KB
/
tokens.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
var app = angular.module('simplefin', []);
app.service('Server', function() {
this.sfin_url = 'https://www.example.com/sfin';
this.put = function(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}
this.get = function(key) {
return JSON.parse(localStorage.getItem(key));
}
this.getAccounts = function() {
var accounts = this.get('accounts');
if (!angular.isArray(accounts)) {
accounts = [
{name: 'Checking', number: '1238394'},
{name: 'Savings', number: '28398403'}
];
this.put('accounts', accounts);
}
return accounts;
}
this.saveAccounts = function(accounts) {
this.put('accounts', accounts);
}
this.getTokens = function() {
var tokens = this.get('tokens');
if (!angular.isArray(tokens)) {
tokens = [];
this.put('tokens', tokens);
}
return tokens;
}
this.saveTokens = function(tokens) {
this.put('tokens', tokens);
}
this.generateSetupToken = function() {
var charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var access_token = '';
var length = 50 + Math.floor(Math.random() * 50);
while (access_token.length < length) {
var r = Math.floor(Math.random() * charset.length);
access_token += charset[r];
}
var setup_token = btoa(this.sfin_url + '\n' + access_token);
return {
'access_token': access_token,
'sfin_url': this.sfin_url,
'setup_token': setup_token,
// pretend to hash right now
'hash': access_token
}
}
})
app.controller('TokenCtrl', function($scope, Server) {
$scope.accountChoice = 'All accounts';
$scope.expirationDate = '2015-01-01';
$scope.accounts = Server.getAccounts();
$scope.tokens = Server.getTokens();
$scope.$watch('tokens', function() {
Server.saveTokens($scope.tokens);
}, true);
$scope.$watch('accounts', function() {
Server.saveAccounts($scope.accounts);
}, true);
$scope.createToken = function() {
var accountChoice = $scope.accountChoice;
var description = $scope.description;
var expirationDate = $scope.expirationDate;
var generated = Server.generateSetupToken();
$scope.tokens.push({
hash: generated.hash,
sfin_url: generated.sfin_url,
accounts: accountChoice,
description: description,
expirationDate: expirationDate,
enabled: true,
last_used_ip: null,
last_used: null
});
// reset form
$scope.accountChoice = 'All accounts';
$scope.description = '';
$scope.expirationDate = '2015-01-01';
$scope.mostRecentToken = generated.setup_token;
};
$scope.isExpired = function(token) {
var now = new Date();
var exp = new Date(token.expirationDate);
return (now >= exp);
}
$scope.deleteToken = function(token, ev) {
var index = $scope.tokens.indexOf(token);
$scope.tokens.splice(index,1);
};
})