Skip to content

Commit

Permalink
fix(silent-signing): more security
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Mar 30, 2022
1 parent 62f6cd6 commit e30289c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
16 changes: 10 additions & 6 deletions packages/context/src/MultiAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {OidcProvider, useOidc, useOidcAccessToken} from "./oidc";
import { configurationIdentityServer} from "./configurations";

const MultiAuth = ( {configurationName, handleConfigurationChange }) => {

const { login, logout, isLogged} = useOidc(configurationName);

return (
<div className="container-fluid mt-3">
<div className="card">
Expand All @@ -22,7 +20,7 @@ const MultiAuth = ( {configurationName, handleConfigurationChange }) => {
</div>
</div>
</div>
)
);
};

if(!sessionStorage.configurationName){
Expand All @@ -34,8 +32,15 @@ export const MultiAuthContainer = () => {
const callBack = window.location.origin+"/multi-auth/authentification/callback2";
const silent_redirect_uri = window.location.origin+"/multi-auth/authentification/silent-callback2";
const configurations = {
"config_1": {...configurationIdentityServer, redirect_uri:callBack, silent_redirect_uri},
"config_2": {...configurationIdentityServer, redirect_uri:callBack, silent_redirect_uri, scope: 'openid profile email api'}
config_1: {...configurationIdentityServer,
redirect_uri:callBack,
silent_redirect_uri,
scope: 'openid profile email api offline_access'
},
config_2: {...configurationIdentityServer,
redirect_uri:callBack,
silent_redirect_uri: "",
scope: 'openid profile email api'}
}
const handleConfigurationChange = (event) => {
const configurationName = event.target.value;
Expand All @@ -51,7 +56,6 @@ export const MultiAuthContainer = () => {
);
};


const DisplayAccessToken = ({configurationName}) => {
const{ accessToken, accessTokenPayload } = useOidcAccessToken(configurationName);

Expand Down
24 changes: 18 additions & 6 deletions packages/context/src/oidc/vanilla/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ import {
TokenRequest
} from '@openid/appauth';
import {NoHashQueryStringUtils} from './noHashQueryStringUtils';
import {initWorkerAsync, sleepAsync} from './initWorker'
import {initWorkerAsync} from './initWorker'
import {MemoryStorageBackend} from "./memoryStorageBackend";
import {initSession} from "./initSession";
import timer from './timer';

const isInIframe = () => {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}

const idTokenPayload = (token) => {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
Expand Down Expand Up @@ -174,8 +182,6 @@ const eventNames = {
silentSigninAsync_error: "silentSigninAsync_error",
}

let isSilentSignin = false;

export class Oidc {
public configuration: Configuration;
public userInfo: null;
Expand Down Expand Up @@ -232,7 +238,9 @@ export class Oidc {
static eventNames = eventNames;

silentSigninCallbackFromIFrame(){
window.top.postMessage(`${this.configurationName}_oidc_tokens:${JSON.stringify(this.tokens)}`, window.location.origin);
if (this.configuration.silent_redirect_uri) {
window.top.postMessage(`${this.configurationName}_oidc_tokens:${JSON.stringify(this.tokens)}`, window.location.origin);
}
}
async silentSigninAsync() {
if (!this.configuration.silent_redirect_uri) {
Expand Down Expand Up @@ -270,7 +278,7 @@ export class Oidc {
iframe.remove();
isResolved = true;
}
}, 8000);
}, 12000);
} catch (e) {
iframe.remove();
reject(e);
Expand Down Expand Up @@ -345,7 +353,11 @@ export class Oidc {
const url = callbackPath || location.pathname + (location.search || '') + (location.hash || '');
const state = url;
this.publishEvent(eventNames.loginAsync_begin, {});
const configuration = this.configuration;
const configuration = this.configuration
// Security we cannot loggin from Iframe
if (!configuration.silent_redirect_uri && isInIframe()) {
throw new Error("Login from iframe is forbidden");
}
let serviceWorker = await initWorkerAsync(configuration.service_worker_relative_url, this.configurationName);
const oidcServerConfiguration = await this.initAsync(configuration.authority);
if(serviceWorker && installServiceWorker) {
Expand Down

0 comments on commit e30289c

Please sign in to comment.