|
1 | 1 | import React, { Component, Fragment } from 'react'; |
2 | 2 | import { PrimaryButton } from '../common'; |
3 | 3 | import { ELEMENT_ID, LOCAL_STORAGE_KEY } from '../../constants/variables'; |
4 | | -import { SSO_LOGIN_ENDPOINT, SSO_BCEID_LOGIN_ENDPOINT } from '../../constants/api'; |
5 | | -import { saveDataInLocalStorage } from '../../utils'; |
6 | | -import { encode as base64encode } from 'base64-arraybuffer'; |
| 4 | +import { SSO_BCEID_LOGIN_ENDPOINT, SSO_IDIR_LOGIN_ENDPOINT } from '../../constants/api'; |
| 5 | +import { getDataFromLocalStorage, saveDataInLocalStorage } from '../../utils'; |
| 6 | +import { generatePKCE } from '../../utils/pkceUtils'; |
7 | 7 |
|
8 | 8 | class SignInButtons extends Component { |
9 | 9 | openNewTab = (link) => window.open(link, '_blank'); |
10 | | - onSigninBtnClick = () => { |
11 | | - this.openNewTab(SSO_LOGIN_ENDPOINT.replace('_CODE_CHALLENGE_VALUE_', this.state.codeVerifierHash)); |
12 | | - }; |
13 | | - onBceidSigninBtnClick = () => { |
14 | | - this.openNewTab(SSO_BCEID_LOGIN_ENDPOINT.replace('_CODE_CHALLENGE_VALUE_', this.state.codeVerifierHash)); |
15 | | - }; |
16 | | - |
17 | | - constructor() { |
18 | | - super(); |
19 | | - this.state = { |
20 | | - codeVerifier: null, |
21 | | - codeVerifierHash: null, |
22 | | - hashComputed: false, |
23 | | - }; |
24 | | - } |
25 | 10 |
|
26 | | - componentDidMount() { |
27 | | - // generate and save new ones, and update the state |
28 | | - let codeVerifier = ''; |
29 | | - |
30 | | - const VALID_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'; |
31 | | - for (let i = 0; i < 128; i++) { |
32 | | - codeVerifier += VALID_CHARS.charAt(Math.floor(Math.random() * VALID_CHARS.length)); |
| 11 | + onSignInButtonClick = () => { |
| 12 | + let loginEndpoint = SSO_BCEID_LOGIN_ENDPOINT.replace( |
| 13 | + '_CODE_CHALLENGE_VALUE_', |
| 14 | + getDataFromLocalStorage(LOCAL_STORAGE_KEY.AUTH_PKCE_CODE).codeVerifierHash, |
| 15 | + ); |
| 16 | + if (getDataFromLocalStorage(LOCAL_STORAGE_KEY.USER)?.ssoId?.startsWith('idir')) { |
| 17 | + loginEndpoint = SSO_IDIR_LOGIN_ENDPOINT.replace( |
| 18 | + '_CODE_CHALLENGE_VALUE_', |
| 19 | + getDataFromLocalStorage(LOCAL_STORAGE_KEY.AUTH_PKCE_CODE).codeVerifierHash, |
| 20 | + ); |
33 | 21 | } |
| 22 | + this.openNewTab(loginEndpoint); |
| 23 | + }; |
34 | 24 |
|
35 | | - const encoder = new TextEncoder(); |
36 | | - const data = encoder.encode(codeVerifier); |
37 | | - crypto.subtle.digest('SHA-256', data).then((hash) => { |
38 | | - // hash complete |
39 | | - |
40 | | - const encodedHash = base64encode(hash).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); |
41 | | - |
42 | | - this.setState({ |
43 | | - ...this.state, |
44 | | - codeVerifier: codeVerifier, |
45 | | - codeVerifierHash: encodedHash, |
46 | | - hashComputed: true, |
47 | | - }); |
48 | | - |
| 25 | + componentDidMount() { |
| 26 | + generatePKCE().then((pkce) => { |
49 | 27 | saveDataInLocalStorage(LOCAL_STORAGE_KEY.AUTH_PKCE_CODE, { |
50 | | - codeVerifier: codeVerifier, |
51 | | - codeVerifierHash: encodedHash, |
| 28 | + codeVerifier: pkce.codeVerifier, |
| 29 | + codeVerifierHash: pkce.codeVerifierHash, |
52 | 30 | }); |
53 | 31 | }); |
54 | 32 | } |
55 | 33 |
|
56 | 34 | render() { |
57 | | - if (!this.state.hashComputed) { |
58 | | - return <Fragment>...</Fragment>; |
| 35 | + if (getDataFromLocalStorage(LOCAL_STORAGE_KEY.USER)?.ssoId?.startsWith('idir')) { |
| 36 | + return ( |
| 37 | + <Fragment> |
| 38 | + <PrimaryButton |
| 39 | + id={ELEMENT_ID.LOGIN_IDIR_BUTTON} |
| 40 | + className="signin__button" |
| 41 | + fluid |
| 42 | + style={{ height: '45px', marginTop: '15px', marginRight: '0' }} |
| 43 | + onClick={this.onSignInButtonClick} |
| 44 | + content="Login using IDIR" |
| 45 | + /> |
| 46 | + </Fragment> |
| 47 | + ); |
59 | 48 | } |
60 | | - |
61 | 49 | return ( |
62 | 50 | <Fragment> |
63 | 51 | <PrimaryButton |
64 | 52 | id={ELEMENT_ID.LOGIN_BCEID_BUTTON} |
65 | 53 | className="signin__button" |
66 | 54 | fluid |
67 | 55 | style={{ height: '45px', marginTop: '15px', marginRight: '0' }} |
68 | | - onClick={this.onBceidSigninBtnClick} |
69 | | - content="Login" |
| 56 | + onClick={this.onSignInButtonClick} |
| 57 | + content="Login using BCeID" |
70 | 58 | /> |
71 | 59 | </Fragment> |
72 | 60 | ); |
|
0 commit comments