Skip to content

Commit

Permalink
feat: add support for logout flow
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Jun 30, 2020
1 parent c4a6431 commit a4b9f97
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 19 deletions.
19 changes: 19 additions & 0 deletions app_auth_logout/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": "app/.auth/logout"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
32 changes: 32 additions & 0 deletions app_auth_logout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { response, validateCookie } = require("../utils");

module.exports = async function (context, req) {
const cookie = req.headers.cookie;
const { post_logout_redirect_uri } = req.query;

if (!cookie || !validateCookie(cookie)) {
return response({
context,
status: 401,
});
}

context.res = response({
context,
status: 302,
cookies: [
{
name: "StaticWebAppsAuthContextCookie",
value: process.env.StaticWebAppsAuthContextCookie,
path: "/",
secure: false,
HttpOnly: false,
domain: "127.0.0.1",
SameSite: "None",
},
],
headers: {
location: `http://127.0.0.1:4242/.redirect/logout?hostName=127.0.0.1`,
},
});
};
19 changes: 19 additions & 0 deletions app_auth_logout_complete/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": "app/.auth/logout/complete"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
40 changes: 40 additions & 0 deletions app_auth_logout_complete/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { response } = require("../utils");

module.exports = async function (context, req) {
context.res = response({
context,
status: 302,
cookies: [
{
name: "StaticWebAppsAuthCookie",
value: "deleted",
path: "/",
secure: false,
HttpOnly: false,
domain: "127.0.0.1",
expires: new Date(1970),
},
{
name: "StaticSitesAuthCookie",
value: "deleted",
path: "/",
secure: false,
HttpOnly: false,
domain: "127.0.0.1",
expires: new Date(1970),
},
{
name: "StaticWebAppsAuthContextCookie",
value: "deleted",
path: "/",
secure: false,
HttpOnly: false,
domain: "127.0.0.1",
expires: new Date(1970),
},
],
headers: {
location: `http://127.0.0.1:4200/`,
},
});
};
2 changes: 1 addition & 1 deletion app_auth_me/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"type": "http",
"direction": "out",
"name": "$return"
"name": "res"
}
]
}
5 changes: 3 additions & 2 deletions app_auth_me/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ module.exports = async function (context, req) {
const cookie = req.headers.cookie;

if (!cookie || !validateCookie(cookie)) {
return response({
context.res = response({
context,
status: 200,
body: {
clientPrincipal: null
}
});
return;
}

return response({
context.res = response({
context,
status: 200,
body: {
Expand Down
19 changes: 19 additions & 0 deletions identity_auth_logout/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/logout"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
21 changes: 21 additions & 0 deletions identity_auth_logout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { response } = require("../utils");

module.exports = async function (context, req) {
context.res = response({
context,
status: 302,
cookies: [
{
name: "AppServiceAuthSession",
value: "deleted",
path: "/",
secure: false,
HttpOnly: false,
expires: new Date(1970),
},
],
headers: {
location: `http://127.0.0.1:4242/.auth/logout/complete`,
},
});
};
19 changes: 19 additions & 0 deletions identity_auth_logout_complete/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/logout/complete"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
24 changes: 24 additions & 0 deletions identity_auth_logout_complete/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { response } = require("../utils");

module.exports = async function (context, req) {
const location = `http://127.0.0.1:4242/app/.auth/logout/complete`;

context.res = response({
context,
status: 302,
cookies: [
{
name: "StaticWebAppsAuthContextCookie",
value: "deleted",
path: "/",
secure: false,
HttpOnly: false,
domain: "127.0.0.1",
expires: new Date(1970),
},
],
headers: {
location,
},
});
};
19 changes: 19 additions & 0 deletions identity_redirect_logout/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": ".redirect/logout"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
26 changes: 26 additions & 0 deletions identity_redirect_logout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { response } = require("../utils");

module.exports = async function (context, req) {
const { hostName, post_logout_redirect_uri = "/" } = req.query;

const location = `http://127.0.0.1:4242/.auth/logout?post_login_redirect_uri=${post_logout_redirect_uri}`;

context.res = response({
context,
status: 302,
cookies: [
{
name: "StaticWebAppsAuthContextCookie",
value: process.env.StaticWebAppsAuthContextCookie,
path: "/",
secure: false,
HttpOnly: false,
domain: "127.0.0.1",
SameSite: "None",
},
],
headers: {
location,
},
});
};
2 changes: 1 addition & 1 deletion identity_redirect_provider/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"methods": [
"get"
],
"route": ".redirect/{provider}"
"route": ".redirect/{provider:regex(aad|github|twitter|google|facebook)}"
},
{
"type": "http",
Expand Down
34 changes: 19 additions & 15 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cookie = require('cookie');
const cookie = require("cookie");

module.exports.response = ({ context, status, headers, cookies, body = "" }) => {
let location;
Expand All @@ -11,16 +11,9 @@ module.exports.response = ({ context, status, headers, cookies, body = "" }) =>
};
}

const res = {
status,
headers,
cookies,
headers: {
status,
"Content-Type": "application/json",
...headers,
},
body:
body = body || null;
if (process.env.DEBUG) {
body =
body ||
JSON.stringify(
{
Expand All @@ -41,12 +34,23 @@ module.exports.response = ({ context, status, headers, cookies, body = "" }) =>
},
null,
2
),
);
}

const res = {
status,
headers,
cookies,
headers: {
status,
//"Content-Type": "application/json",
...headers,
},
body,
};
return res;
};


module.exports.validateCookie = (cookieValue) => {
const cookies = cookie.parse(cookieValue);
console.log(JSON.stringify(cookies));
Expand All @@ -56,5 +60,5 @@ module.exports.validateCookie = (cookieValue) => {
return cookies.StaticWebAppsAuthCookie === process.env.StaticWebAppsAuthCookie;
}

return false;
}
return false;
};

0 comments on commit a4b9f97

Please sign in to comment.