-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
hydra.js
107 lines (99 loc) · 3.38 KB
/
hydra.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
var fetch = require('node-fetch')
var querystring = require('querystring');
var hydraUrl = process.env.HYDRA_ADMIN_URL
var mockTlsTermination = {}
if (process.env.MOCK_TLS_TERMINATION) {
mockTlsTermination = {
'X-Forwarded-Proto': 'https'
}
}
// A little helper that takes type (can be "login" or "consent") and a challenge and returns the response from ORY Hydra.
function get(flow, challenge) {
const url = new URL('/oauth2/auth/requests/' + flow, hydraUrl)
url.search = querystring.stringify({[flow + '_challenge']: challenge})
return fetch(
url.toString(),
{
method: 'GET',
headers: {
...mockTlsTermination
}
}
)
.then(function (res) {
if (res.status < 200 || res.status > 302) {
// This will handle any errors that aren't network related (network related errors are handled automatically)
return res.json().then(function (body) {
console.error('An error occurred while making a HTTP request: ', body)
return Promise.reject(new Error(body.error.message))
})
}
return res.json();
});
}
// A little helper that takes type (can be "login" or "consent"), the action (can be "accept" or "reject") and a challenge and returns the response from ORY Hydra.
function put(flow, action, challenge, body) {
const url = new URL('/oauth2/auth/requests/' + flow + '/' + action, hydraUrl)
url.search = querystring.stringify({[flow + '_challenge']: challenge})
return fetch(
// Joins process.env.HYDRA_ADMIN_URL with the request path
url.toString(),
{
method: 'PUT',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
...mockTlsTermination
}
}
)
.then(function (res) {
if (res.status < 200 || res.status > 302) {
// This will handle any errors that aren't network related (network related errors are handled automatically)
return res.json().then(function (body) {
console.error('An error occurred while making a HTTP request: ', body)
return Promise.reject(new Error(body.error.message))
})
}
return res.json();
});
}
var hydra = {
// Fetches information on a login request.
getLoginRequest: function (challenge) {
return get('login', challenge);
},
// Accepts a login request.
acceptLoginRequest: function (challenge, body) {
return put('login', 'accept', challenge, body);
},
// Rejects a login request.
rejectLoginRequest: function (challenge, body) {
return put('login', 'reject', challenge, body);
},
// Fetches information on a consent request.
getConsentRequest: function (challenge) {
return get('consent', challenge);
},
// Accepts a consent request.
acceptConsentRequest: function (challenge, body) {
return put('consent', 'accept', challenge, body);
},
// Rejects a consent request.
rejectConsentRequest: function (challenge, body) {
return put('consent', 'reject', challenge, body);
},
// Fetches information on a logout request.
getLogoutRequest: function (challenge) {
return get('logout', challenge);
},
// Accepts a logout request.
acceptLogoutRequest: function (challenge) {
return put('logout', 'accept', challenge, {});
},
// Reject a logout request.
rejectLogoutRequest: function (challenge) {
return put('logout', 'reject', challenge, {});
},
};
module.exports = hydra;