Skip to content

Commit

Permalink
feat: add .auth/login/{provider}/callback rule
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Jun 26, 2020
1 parent 87299e1 commit 31b2ac8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
19 changes: 19 additions & 0 deletions identity_auth_login_provider_callback/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get"
],
"route": ".auth/login/{provider}/callback"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
52 changes: 52 additions & 0 deletions identity_auth_login_provider_callback/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { response } = require("../utils");
const { default: fetch } = require("node-fetch");

module.exports = async function (context, req) {
let { state, code } = req.query;

state = decodeURIComponent(state);

const client_id = process.env.GITHUB_CLIENT_ID;
const client_secret = process.env.GITHUB_CLIENT_SECRET;
const oauthUri = `https://github.com/login/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&code=${code}&state=${state}`;
const githubOauthResponse = await fetch(oauthUri, {
method: "POST",
headers: {
accept: "application/json",
},
});

const token = await githubOauthResponse.json();

const location = "http://localhost:4242/.auth/login/done";
context.res = response({
context,
status: 302,
cookies: [
{
name: "Nonce",
value: "deleted",
path: "/",
expires: new Date('Thu, 01 Jan 1970 00:00:00 GMT'),
},
{
name: "RedirectCount",
value: "deleted",
path: "/",
expires: new Date('Thu, 01 Jan 1970 00:00:00 GMT'),
},
{
name: "AppServiceAuthSession",
value: "UUUUU",
path: "/",
secure: true,
HttpOnly: true,
SameSite: "None",
},
],
headers: {
location,
token
},
});
};

0 comments on commit 31b2ac8

Please sign in to comment.