-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
117 lines (99 loc) · 3.92 KB
/
functions.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
const handleACPKCEAuthRequest = require('./fn/handleACPKCEAuthRequest')
const handleACAuthRequest = require('./fn/handleACAuthRequest')
const handleImplicitAuthRequest = require('./fn/handleImplicitAuthRequest')
const handleACPKCESigninRequest = require('./fn/handleACPKCESigninRequest')
const handleACSigninRequest = require('./fn/handleACSigninRequest')
const handleImplictSigninRequest = require('./fn/handleImplictSigninRequest')
const handleROPCTokenRequest = require('./fn/handleROPCTokenRequest')
const handleACTokenRequest = require('./fn/handleACTokenRequest')
const handleACPKCETokenRequest = require('./fn/handleACPKCETokenRequest')
const handleCCTokenRequest = require('./fn/handleCCTokenRequest')
const returnError = require('./fn/returnError')
const {handleXummSignin} = require('./fn/xummSignin')
module.exports = {
auth (req, res) {
console.log('auth')
switch (req.query.response_type) {
case ('code'):
if (req.query.code_challenge && req.query.code_challenge_method) {
handleACPKCEAuthRequest(req, res)
} else if (!req.query.code_challenge && !req.query.code_challenge_method) {
handleACAuthRequest(req, res)
} else {
returnError(req, res, 'invalid_request', 'Required parameters are missing in the request.', 400, {})
}
break
case ('token'):
handleImplicitAuthRequest(req, res)
break
default:
returnError(req, res, 'invalid_request', 'Grant type is invalid or missing.', 400, {})
break
}
},
token (req, res) {
// If BASIC auth, mock client_id and client_secret
const b64auth = ((req.headers?.authorization || '').trim() || '').split(' ').reverse()[0] || ''
const strauth = Buffer.from(b64auth, 'base64').toString()
const splitIndex = strauth.indexOf(':')
console.log({b64auth, strauth, splitIndex})
let login
let password
if (splitIndex > -1 && !req.body?.client_id && !req.body?.client_secret) {
login = strauth.substring(0, splitIndex)
password = strauth.substring(splitIndex + 1)
// console.log({login, password})
req.body.client_id = login
req.body.client_secret = password
}
switch (req.body.grant_type) {
case 'password':
handleROPCTokenRequest(req, res)
break
case 'authorization_code':
if (req.body.client_secret && !req.body.code_verifier) {
handleACTokenRequest(req, res)
break
}
if (req.body.code_verifier) {
handleACPKCETokenRequest(req, res)
break
}
returnError(req, res, 'invalid_request', 'Client secret and code verifier are exclusive to each other.', 400, {})
break
case 'client_credentials':
handleCCTokenRequest(req, res)
break
default:
returnError(req, res, 'invalid_request', 'Grant type is invalid or missing.', 400, {})
break
}
},
signin (req, res) {
// Todo: handle XUMM Sign In request
const _xummHandled = handleXummSignin(req, res)
;(_xummHandled || Promise.resolve(_xummHandled)).then(xummHandled => {
// console.log('typeof xummHandled', typeof xummHandled, xummHandled)
if (typeof xummHandled !== 'undefined') {
return xummHandled
}
// DEBUG, Original boddy, with appended data from the Sign In request (xummSignin.js)
// console.log('req?.body', req.body)
switch (req?.body?.response_type ? req.body.response_type : req?.query?.response_type) {
case ('code'):
if (!req.body.code_challenge) {
handleACSigninRequest(req, res)
} else {
handleACPKCESigninRequest(req, res)
}
break
case ('token'):
handleImplictSigninRequest(req, res)
break
default:
returnError(req, res, 'invalid_request', 'Grant type is invalid or missing.', 400, {})
break
}
})
}
}