-
Notifications
You must be signed in to change notification settings - Fork 145
/
client.js
150 lines (140 loc) · 3.96 KB
/
client.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
var
error = require('./../error');
/**
* Typical client schema:
* _id: { type: "object", required: true, unique: true },
* name: { type: "string", required: true },
* secret: { type: "string", required: true },
* uri: { type: "string", required: false },
* scope: { type: "array", required: false,
* items: { type: "string", enum: ["possible", "scope", "values"] },
* },
* grants: { type: "array", required: false,
* items: { type: "string", enum: ["authorization_code", "implicit", "password", "client_credentials"] }
* }
*/
/**
* Gets clients primary key
*
* @param client {Object} Client object
*/
module.exports.getId = function(client) {
throw new error.serverError('Client model method "getId" is not implemented');
};
/**
* Gets clients secret
*
* @param client {Object} Client object
*/
module.exports.getSecret = function(client) {
throw new error.serverError('Client model method "getSecret" is not implemented');
};
/**
* Gets clients redirect uri
*
* @param client {Object} Client object
*/
module.exports.getRedirectUri = function(client) {
throw new error.serverError('Client model method "getRedirectUri" is not implemented');
};
/**
* Checks redirect uri for the client
*
* @param client {Object} Client object
* @param client {String} Redirect URI to be checked
*/
module.exports.checkRedirectUri = function(client, redirectUri) {
/**
* For example:
* // for single redirect uri per client
* return (redirectUri.indexOf(getRedirectUri(client)) === 0 &&
* redirectUri.replace(getRedirectUri(client), '').indexOf('#') === -1);
*
* // for multiple redirect uris per client
* return (getRedirectUri(client).indexOf(redirectUri) !== -1);
*/
throw new error.serverError('Client model method "checkRedirectUri" is not implemented');
};
/**
* Fetches client object by primary key
* Should be implemented with server logic
*
* @param clientId {String} Unique identifier
* @param cb {Function} Function callback ->(error, object)
*/
module.exports.fetchById = function(clientId, cb) {
/**
* For example:
*
*/
throw new error.serverError('Client model method "fetchById" is not implemented');
};
/**
* Checks secret for the client
* Function arguments MAY be different
*
* @param client {Object} Client object
* @param secret {String} Password to be checked
*/
module.exports.checkSecret = function(client, secret, cb) {
/**
* For example:
* superHashFunction(secret, function(err, hash){
* if(err){
* return cb(err);
* }
*
* cb(null,client.secret === hash)
* });
*
* OR for sync hash function
*
* cb(null, client.secret != superHashFunction(secret));
*
*/
throw new error.serverError('Client model method "checkSecret" is not implemented');
};
/**
* Checks grant type permission for the client
* Default: do not check it
* Function arguments MAY be different
*
* @param client {Object} Client object
* @param grant {String} Grant type to be checked for
*/
module.exports.checkGrantType = function(client, grant) {
/**
* For example:
* if (client.grants.indexOf(grant) !== -1) return true;
* else false;
*/
return true;
};
/**
* Checks scope permission for the client
* Default: do not check it, return empty array
* Function arguments MAY be different
*
* @param client {Object} Client object
* @param scope {String} Scope string (space delimited) passed via parameters
* @return {Array|boolean} Return checked scope array or false
*/
module.exports.checkScope = function(client, scope) {
/**
* For example:
* scope.forEach(function(item) {
* if (scope.indexOf(item) == -1) return false;
* });
* return scope;
*/
return [];
};
/**
* Transforms scope body parameter to scope array
*
* @param scope
*/
module.exports.transformScope = function(scope) {
if (!scope) return [];
return scope.split(' ');
};