fix: Authorisation flows working.

This commit is contained in:
Sam Tolmay 2021-10-21 15:51:37 +02:00
parent 3d910d0583
commit 5b32ca86ba
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
12 changed files with 33 additions and 60 deletions

View File

@ -24,12 +24,14 @@ import unsetAuthorizationCookie from './unsetAuthorizationCookie';
function parseLogoutUrlNunjucks(context, { openIdConfig, idToken }) {
const template = nunjucksFunction(openIdConfig.logoutRedirectUri);
return template({
id_token_hint: idToken,
client_id: openIdConfig.clientId,
openid_domain: openIdConfig.domain,
host: encodeURIComponent(`${context.protocol}://${context.host}`),
});
return {
openIdLogoutUrl: template({
id_token_hint: idToken,
client_id: openIdConfig.clientId,
openid_domain: openIdConfig.domain,
host: encodeURIComponent(`${context.protocol}://${context.host}`),
}),
};
}
function openIdLogoutUrl(context, { idToken }) {
@ -37,7 +39,7 @@ function openIdLogoutUrl(context, { idToken }) {
unsetAuthorizationCookie(context);
const openIdConfig = getOpenIdConfig(context);
if (!type.isString(openIdConfig.logoutRedirectUri)) return null;
if (!type.isString(openIdConfig.logoutRedirectUri)) return { openIdLogoutUrl: null };
return parseLogoutUrlNunjucks(context, { openIdConfig, idToken });
} catch (error) {

View File

@ -21,15 +21,6 @@ module.exports = {
module: {
rules: [
// TODO: FIXME: do NOT webpack 5 support with this
// x-ref: https://github.com/webpack/webpack/issues/11467
// waiting for babel fix: https://github.com/vercel/next.js/pull/17095#issuecomment-692435147
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.jsx?$/,
loader: 'babel-loader',

View File

@ -18,16 +18,14 @@ import React, { Suspense } from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { ErrorBoundary } from '@lowdefy/block-tools';
import { get } from '@lowdefy/helpers';
import createLogin from './auth/createLogin';
import createLogout from './auth/createLogout';
import DisplayMessage from './page/DisplayMessage';
import Page from './page/Page';
import useRootData from './swr/useRootData';
import parseJwt from './auth/parseJwt';
import getCookie from './utils/getCookie';
import Page from './page/Page';
import parseJwt from './auth/parseJwt';
import useRootData from './swr/useRootData';
const lowdefy = {
basePath: window.lowdefy.basePath,
@ -57,15 +55,18 @@ const RootData = ({ children, lowdefy }) => {
lowdefy.homePageId = data.homePageId;
lowdefy.menus = data.menus;
// Make a copy to avoid immutable error when calling setGlobal.
lowdefy.lowdefyGlobal = JSON.parse(JSON.stringify(get(data, 'lowdefyGlobal', { default: {} })));
// TODO We used to make a copy to avoid immutable error when calling setGlobal using Apollo Client.
// Check if still needed
// lowdefy.lowdefyGlobal = JSON.parse(JSON.stringify(get(data, 'lowdefyGlobal', { default: {} })));
lowdefy.lowdefyGlobal = data.lowdefyGlobal;
if (data.authenticated) {
const idToken = getCookie('idToken');
const idToken = getCookie(lowdefy, { cookieName: 'idToken' });
if (!idToken) {
// This is async, so maybe we need a useEffect?
lowdefy.auth.logout();
// Throw promise to suspend till user is logged out.
throw new Promise(() => {});
}
// eslint-disable-next-line no-unused-vars
const { iat, exp, aud, iss, ...user } = parseJwt(idToken);

View File

@ -18,7 +18,6 @@ import request from '../utils/request';
function createLogout(lowdefy) {
async function logout() {
console.log('logout');
lowdefy.user = {};
const idToken = lowdefy.localStorage.getItem('idToken');
lowdefy.localStorage.setItem(`idToken`, '');
@ -31,8 +30,6 @@ function createLogout(lowdefy) {
},
});
console.log('data', data);
lowdefy.window.location.href = data.openIdLogoutUrl || lowdefy.window.location.origin;
}

View File

@ -14,9 +14,8 @@
limitations under the License.
*/
function getCookie(name) {
// TODO: Should we use document from lowdefy here (for testing)?
const match = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)');
function getCookie({ document }, { cookieName }) {
const match = document.cookie.match('(^|;)\\s*' + cookieName + '\\s*=\\s*([^;]+)');
if (!match) return null;
return match.pop();
}

View File

@ -21,15 +21,6 @@ module.exports = {
},
module: {
rules: [
// TODO: FIXME: do NOT webpack 5 support with this
// x-ref: https://github.com/webpack/webpack/issues/11467
// waiting for babel fix: https://github.com/vercel/next.js/pull/17095#issuecomment-692435147
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.jsx?$/,
loader: 'babel-loader',

View File

@ -20,15 +20,6 @@ module.exports = {
},
module: {
rules: [
// TODO: FIXME: do NOT webpack 5 support with this
// x-ref: https://github.com/webpack/webpack/issues/11467
// waiting for babel fix: https://github.com/vercel/next.js/pull/17095#issuecomment-692435147
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.jsx?$/,
loader: 'babel-loader',

View File

@ -17,9 +17,11 @@
import { homePageId } from '@lowdefy/api';
async function homeHtmlHandler(request, reply) {
// TODO: If user has configured homePageId, mount homePage
// else redirect
const home = await homePageId(request.lowdefyContext);
if (!home) {
reply.redirect('/404');
return;
}
reply.redirect(`/${home}`);
}

View File

@ -18,6 +18,7 @@ import { openIdAuthorizationUrl } from '@lowdefy/api';
async function openIdAuthorizationUrlHandler(request, reply) {
const { authUrlQueryParams, pageId, urlQuery } = request.body;
const data = await openIdAuthorizationUrl(request.lowdefyContext, {
authUrlQueryParams,
pageId,

View File

@ -15,6 +15,7 @@
*/
import { homePageId, openIdCallback, AuthenticationError } from '@lowdefy/api';
import { type, urlQuery as urlQueryFn } from '@lowdefy/helpers';
async function openIdCallbackHandler(request, reply) {
try {
@ -28,10 +29,7 @@ async function openIdCallbackHandler(request, reply) {
if (!code || !state) throw new AuthenticationError('Authentication error.');
// Authentication an idToken cookies are set by openIdCallback function.
let {
pageId,
//urlQuery
} = await openIdCallback(request.lowdefyContext, {
let { pageId, urlQuery } = await openIdCallback(request.lowdefyContext, {
code,
state,
});
@ -39,10 +37,9 @@ async function openIdCallbackHandler(request, reply) {
if (!pageId) {
pageId = await homePageId(request.lowdefyContext);
}
const templateUrlQuery = type.isNone(urlQuery) ? '' : `?${urlQueryFn.stringify(urlQuery)}`;
// TODO: Need to set urlQuery;
reply.redirect(`/${pageId}`);
reply.redirect(`/${pageId}${templateUrlQuery}`);
} catch (error) {
console.log(error);
console.log(error.message);

View File

@ -18,10 +18,10 @@ import { openIdLogoutUrl } from '@lowdefy/api';
async function openIdLogoutUrlHandler(request, reply) {
const { idToken } = request.body;
const page = await openIdLogoutUrl(request.lowdefyContext, {
const data = await openIdLogoutUrl(request.lowdefyContext, {
idToken,
});
reply.send(page);
reply.send(data);
}
export default openIdLogoutUrlHandler;

View File

@ -21,6 +21,7 @@ async function pageHtmlHandler(request, reply) {
const page = await pageHtml(request.lowdefyContext, { pageId });
if (!page) {
reply.redirect('/404');
return;
}
reply.type('text/html');
reply.send(page);