Skip to content

Commit 416f91f

Browse files
committed
feat: support other auth providers (use fake impl only)
1 parent 446cbb6 commit 416f91f

File tree

4 files changed

+67
-30
lines changed

4 files changed

+67
-30
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "function",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get"
10+
],
11+
"route": "fake/{provider:regex(aad|github|twitter|google|facebook)}/oauth/authorize"
12+
},
13+
{
14+
"type": "http",
15+
"direction": "out",
16+
"name": "res"
17+
}
18+
]
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { response, ɵɵUseGithubDevToken } = require("../../utils");
2+
const SWA_EMU_AUTH_URI = process.env.SWA_EMU_AUTH_URI || `http://localhost:4242`;
3+
4+
module.exports = async function (context, req) {
5+
const { provider } = context.bindingData;
6+
const { redirect_uri, state, client_id } = req.query;
7+
8+
console.log("+++++++++");
9+
console.log(req.query);
10+
console.log("+++++++++");
11+
12+
location = `${redirect_uri}?code=CODE&state=${state}`;
13+
14+
context.res = response({
15+
context,
16+
status: 302,
17+
headers: {
18+
location,
19+
},
20+
});
21+
};

src/auth/identity_auth_login_provider/index.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,34 @@ module.exports = async function (context, req) {
55
const { provider } = context.bindingData;
66
const { post_login_redirect_uri } = req.query;
77

8-
const redirect_uri = `${SWA_EMU_AUTH_URI}/.auth/login/${provider}/callback&state=${encodeURIComponent(`redir=${post_login_redirect_uri}&nonce=${context.invocationId}`)}`;
8+
const redirect_uri = `${SWA_EMU_AUTH_URI}/.auth/login/${provider}/callback&state=${encodeURIComponent(
9+
`redir=${post_login_redirect_uri}&nonce=${context.invocationId}`
10+
)}`;
11+
let location = "";
12+
let client_id = "fake-client-id";
913

10-
let client_id = process.env.GITHUB_CLIENT_ID;
14+
switch (provider) {
15+
case "github":
16+
client_id = process.env.GITHUB_CLIENT_ID;
1117

12-
//**** GITHUB NOTICE */
13-
if (!client_id) {
14-
({ client_id } = await ɵɵUseGithubDevToken());
15-
}
16-
//**** GITHUB NOTICE */
18+
//**** GITHUB NOTICE: start */
19+
if (!client_id) {
20+
({ client_id } = await ɵɵUseGithubDevToken());
21+
}
22+
//**** GITHUB NOTICE: end */
23+
location = `https://github.com/login/oauth/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}`;
24+
25+
break;
1726

18-
const location = `https://github.com/login/oauth/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}`;
27+
case "twitter":
28+
case "google":
29+
case "facebook":
30+
case "aad":
31+
location = `${SWA_EMU_AUTH_URI}/fake/${provider}/oauth/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}`;
32+
break;
33+
default:
34+
// TODO: throw http error
35+
}
1936

2037
context.res = response({
2138
context,

src/auth/identity_auth_login_provider_callback/index.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
1-
const { response, ɵɵUseGithubDevToken } = require("../../utils");
2-
const { default: fetch } = require("node-fetch");
1+
const { response } = require("../../utils");
32
const SWA_EMU_AUTH_URI = process.env.SWA_EMU_AUTH_URI || `http://localhost:4242`;
43

54
module.exports = async function (context, req) {
6-
let { state, code } = req.query;
7-
state = decodeURIComponent(state);
5+
let { state, code, nonce } = req.query;
86

9-
let client_id = process.env.GITHUB_CLIENT_ID;
10-
let client_secret = process.env.GITHUB_CLIENT_SECRET;
11-
12-
//**** GITHUB NOTICE */
13-
if (!client_id || !client_secret) {
14-
({ client_id, client_secret } = await ɵɵUseGithubDevToken());
15-
}
16-
//**** GITHUB NOTICE */
17-
18-
const oauthUri = `https://github.com/login/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&code=${code}&state=${state}`;
19-
const githubOauthResponse = await fetch(oauthUri, {
20-
method: "POST",
21-
headers: {
22-
accept: "application/json",
23-
},
24-
});
25-
26-
const token = await githubOauthResponse.json();
277
const location = `${SWA_EMU_AUTH_URI}/.auth/login/done`;
288
context.res = response({
299
context,

0 commit comments

Comments
 (0)