Untitled
unknown
plain_text
a year ago
3.0 kB
2
Indexable
Never
import React, { Component } from 'react'; import Axios from 'axios'; import { connect } from 'react-redux'; import AuthorizationUtils from './AuthorizationUtils'; import OpenIdConfig from '../../app/OpenIdConfig'; const queryString = require('query-string'); class Authorization extends Component { REDIRECT_URI = `${AuthorizationUtils.getAppFrontendURL()}/user/authorization`; SCOPE = 'openid api'; RESPONSE_TYPE = 'code'; GRANT_TYPE = 'authorization_code'; async componentDidMount() { try { await AuthorizationUtils.loadConfiguration(); this.CLIENT_ID = OpenIdConfig.values.clientId ? OpenIdConfig.values.clientId : this.CLIENT_ID; this.REDIRECT_URI = OpenIdConfig.values.redirectUri ? OpenIdConfig.values.redirectUri : this.REDIRECT_URI; this.SCOPE = OpenIdConfig.values.scope ? OpenIdConfig.values.scope : this.SCOPE; this.RESPONSE_TYPE = OpenIdConfig.values.responseType ? OpenIdConfig.values.responseType : this.RESPONSE_TYPE; this.GRANT_TYPE = OpenIdConfig.values.grantType ? OpenIdConfig.values.grantType : this.GRANT_TYPE; } catch (error) { OpenIdConfig.values = {}; } // let urlParameters = queryString.parse(this.props.location.search); let urlParameters = new URLSearchParams(this.props.location.search); if (!urlParameters.get('code') && !urlParameters.get('error')) { window.location = `${OpenIdConfig.values.openIdFrontendUrl}/oidc/authorize?scope=${this.SCOPE}&client_id=${this.CLIENT_ID}&response_type=${this.RESPONSE_TYPE}&redirect_uri=${this.REDIRECT_URI}`; } let body = { code: urlParameters.get('code'), grant_type: this.GRANT_TYPE, redirect_uri: this.REDIRECT_URI, state: urlParameters.get('state'), }; let config = { headers: {}, auth: { username: this.CLIENT_ID, }, }; try { let tokenResponse = await Axios.post( `${OpenIdConfig.values.openIdBackendUrl}/openid/token`, new URLSearchParams(body).toString(), config ); localStorage.setItem('access_token', tokenResponse.data.access_token); localStorage.setItem('refresh_token', tokenResponse.data.refresh_token); localStorage.setItem('id_token', tokenResponse.data.id_token); this.props.history.push('/'); } catch (error) { this.props.history.push('/'); } } render() { return <div></div>; } } function mapStateToProps({ auth }) { return {}; } export default connect(mapStateToProps)(Authorization);