Skip to content

Commit

Permalink
Add Login for OIDC client
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Aug 19, 2024
1 parent 0e9e7a4 commit 6144088
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 52 deletions.
121 changes: 69 additions & 52 deletions src/auth/FormElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ export default class GmfAuthForm extends GmfBaseElement {
@state() private allowPasswordReset = false;
@state() private changingPassword = false;
@state() private userMustChangeItsPassword = false;
@state() private openIdConnectUrl = '';
@state() private error = false;
@state() private otpImage = '';
@state() private gmfUser: User = null;
@state() private customCSS_ = '';
private changingPasswordUsername_ = '';
private initialApplicationUrl = window.location.href;
private currentApplicationUrl = window.location.href;
private openIdConnectBaseUrl = '';

connectedCallback(): void {
super.connectedCallback();
Expand All @@ -79,6 +83,16 @@ export default class GmfAuthForm extends GmfBaseElement {
this.loginInfoMessage = message;
},
}),
setInterval(() => {

Check failure on line 86 in src/auth/FormElement.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Argument of type 'Timeout' is not assignable to parameter of type 'Subscription'.
this.currentApplicationUrl = window.location.href;
const applicationUrl = this.loginInfoMessage
? this.currentApplicationUrl
: this.initialApplicationUrl;
const params = new URLSearchParams({
came_from: applicationUrl,
});
this.openIdConnectUrl = `${this.openIdConnectBaseUrl}?${params.toString()}`;
}, 200),
);

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
Expand All @@ -103,6 +117,7 @@ export default class GmfAuthForm extends GmfBaseElement {
this.twoFactorAuth = configuration.gmfTwoFactorAuth;
this.allowPasswordChange = configuration.gmfAuthenticationConfig.allowPasswordChange;
this.allowPasswordReset = configuration.gmfAuthenticationConfig.allowPasswordReset;
this.openIdConnectBaseUrl = configuration.gmfOidcLoginUrl;
if (configuration.gmfCustomCSS && configuration.gmfCustomCSS.authentication !== undefined) {
this.customCSS_ = configuration.gmfCustomCSS.authentication;
}
Expand Down Expand Up @@ -168,7 +183,7 @@ export default class GmfAuthForm extends GmfBaseElement {
</div>
<div class="form-group">
<input
?hidden="${!this.allowPasswordChange}"
?hidden="${!(this.allowPasswordChange && this.gmfUser.login_type === 'local')}"
type="button"
class="form-control btn btn-default"
value=${i18next.t('Change password')}
Expand Down Expand Up @@ -196,58 +211,60 @@ export default class GmfAuthForm extends GmfBaseElement {
`
: ''}
${this.gmfUser.username === null && !this.changingPassword
? html`
<div>
<form name="loginForm" role="form" @submit=${(evt: Event) => this.login(evt)}>
<div class="form-group">
<slot name="gmf-auth-login"></slot>
</div>
<div class="form-group">
<slot name="gmf-auth-password"></slot>
</div>
${this.twoFactorAuth
? html`
<div class="form-group">
${i18next.t('The following field should be kept empty on first login:')}
<input
type="text"
class="form-control"
name="otp"
autocomplete="one-time-code"
placeholder=${i18next.t('Authentication code')}
/>
</div>
`
: ''}
<div class="form-group">
<input type="submit" class="form-control btn prime" value=${i18next.t('Connect')} />
</div>
${this.isLoading
? html`
<div class="login-spinner">
<i class="fa fa-spin"
>${
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
unsafeSVG(loadingSvg)
}</i
>
</div>
`
? this.gmfUser.auth_type == 'local'

Check failure on line 214 in src/auth/FormElement.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Property 'auth_type' does not exist on type 'User'.
? html`
<div>
<form name="loginForm" role="form" @submit=${(evt: Event) => this.login(evt)}>
<div class="form-group">
<slot name="gmf-auth-login"></slot>
</div>
<div class="form-group">
<slot name="gmf-auth-password"></slot>
</div>
${this.twoFactorAuth
? html`
<div class="form-group">
${i18next.t('The following field should be kept empty on first login:')}
<input
type="text"
class="form-control"
name="otp"
autocomplete="one-time-code"
placeholder=${i18next.t('Authentication code')}
/>
</div>
`
: ''}
<div class="form-group">
<input type="submit" class="form-control btn prime" value=${i18next.t('Connect')} />
</div>
${this.isLoading
? html`
<div class="login-spinner">
<i class="fa fa-spin"
>${
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
unsafeSVG(loadingSvg)
}</i
>
</div>
`
: ''}
<div ?hidden="${!this.allowPasswordReset}" class="form-group">
<a @click=${(evt: Event) => this.resetPassword(evt)} href=""
>${i18next.t('Password forgotten?')}</a
>
</div>
</form>
${this.resetPasswordShown
? html` <div class="alert alert-info">
${i18next.t('A new password has just been sent to you by e-mail.')}
</div>`
: ''}
<div ?hidden="${!this.allowPasswordReset}" class="form-group">
<a @click=${(evt: Event) => this.resetPassword(evt)} href=""
>${i18next.t('Password forgotten?')}</a
>
</div>
</form>
${this.resetPasswordShown
? html` <div class="alert alert-info">
${i18next.t('A new password has just been sent to you by e-mail.')}
</div>`
: ''}
</div>
`
</div>
`
: html`<a href="${this.openIdConnectUrl}">${i18next.t('Connect')}</a>`
: ''}
${this.changingPassword
? html`
Expand Down
13 changes: 13 additions & 0 deletions src/auth/component.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ const login = user.getEmptyUserProperties();
login.username = 'George';
WithUser.args.user = login;

export const EmptyOidc: any = Template.bind({});
Empty.args = {...defaultProperties};
const loginEmptyOidc = user.getEmptyUserProperties();
loginEmptyOidc.auth_type = 'oidc';

Check failure on line 85 in src/auth/component.stories.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Property 'auth_type' does not exist on type 'User'.
Empty.args.user = loginEmptyOidc;

export const WithUserOidc: any = Template.bind({});
WithUser.args = {...defaultProperties};
const loginOidc = user.getEmptyUserProperties();
loginOidc.auth_type = 'oidc';

Check failure on line 91 in src/auth/component.stories.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Property 'auth_type' does not exist on type 'User'.
login.username = 'George OIDC';
WithUser.args.user = loginOidc;

/**
* @returns The HTML of the story
*/
Expand Down
1 change: 1 addition & 0 deletions srcapi/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,7 @@ export type Configuration = {
gmfI18nextConfiguration: InitOptions;
pytreeLidarprofileJsonUrl: pytreeLidarprofileJsonUrl;
gmfDatasourceOptions: gmfDatasourceOptions;
gmfOidcLoginUrl: string;
};

export type APIConfig = {
Expand Down
4 changes: 4 additions & 0 deletions srcapi/store/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export interface User {
* The two-factor authentication secret on first login
*/
two_factor_totp_secret: string;
/**
* The server-side login type (oidc or local)
*/
login_type?: string;
}

export enum UserState {
Expand Down

0 comments on commit 6144088

Please sign in to comment.