// Copyright 2020 Red Hat, Inc // // Licensed under the Apache License, Version 2.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import { Accordion, AccordionItem, AccordionToggle, AccordionContent, Button, ButtonVariant, ClipboardCopy, ClipboardCopyVariant, Modal, ModalVariant } from '@patternfly/react-core' import { UserIcon, SignInAltIcon, SignOutAltIcon, HatWizardIcon } from '@patternfly/react-icons' import * as moment from 'moment' import { apiUrl } from '../../api' import { fetchUserACL } from '../../actions/user' import { withAuth } from 'oidc-react' import { getHomepageUrl } from '../../api' class AuthContainer extends React.Component { static propTypes = { user: PropTypes.object, tenant: PropTypes.object, dispatch: PropTypes.func.isRequired, timezone: PropTypes.string.isRequired, info: PropTypes.object, auth: PropTypes.object, // Props coming from withAuth userManager: PropTypes.object, signIn: PropTypes.func, signOut: PropTypes.func, } constructor(props) { super(props) this.state = { isModalOpen: false, showZuulClientConfig: false, isSessionExpiredModalOpen: false, } this.handleModalToggle = () => { this.setState(({ isModalOpen }) => ({ isModalOpen: !isModalOpen })) } this.handleSessionExpiredModalToggle = () => { this.setState(({ isSessionExpiredModalOpen }) => ({ isSessionExpiredModalOpen: !isSessionExpiredModalOpen })) } this.handleConfigToggle = () => { this.setState(({ showZuulClientConfig }) => ({ showZuulClientConfig: !showZuulClientConfig })) } this.onAccessTokenExpired = () => { // If the token has expired, show the modal console.debug('Token expired') this.setState(() => ({ isSessionExpiredModalOpen: true, isModalOpen: false, })) } this.onUserLoaded = () => { // If another tab logged in while our expired modal is shown, go // ahead and clear it. console.debug('User signed in') this.setState(() => ({ isSessionExpiredModalOpen: false, })) } } clickOnSignIn() { const redirect_target = window.location.href.slice(getHomepageUrl().length) localStorage.setItem('zuul_auth_redirect', redirect_target) this.props.signIn() } componentDidMount() { const { user, tenant } = this.props this.props.userManager.events.addAccessTokenExpired(this.onAccessTokenExpired) this.props.userManager.events.addUserLoaded(this.onUserLoaded) if (user.data) { console.log('Refreshing ACL', user.tenant, tenant.name) this.props.dispatch(fetchUserACL(tenant? tenant.name : null)) } } componentWillUnmount() { this.props.userManager.events.removeAccessTokenExpired(this.onAccessTokenExpired) this.props.userManager.events.removeUserLoaded(this.onUserLoaded) } componentDidUpdate() { const { user, tenant } = this.props // Make sure the token is current and the tenant is up to date. if (user.data && user.tenant !== tenant.name) { console.log('Refreshing ACL', user.tenant, tenant.name) this.props.dispatch(fetchUserACL(tenant? tenant.name : null)) } } ZuulClientConfig() { const { user, tenant } = this.props let ZCconfig ZCconfig = '[' + tenant.name + ']\n' ZCconfig = ZCconfig + 'url=' + apiUrl.slice(0, -4) + '\n' ZCconfig = ZCconfig + 'tenant=' + tenant.name + '\n' ZCconfig = ZCconfig + 'auth_token=' + user.token + '\n' return ZCconfig } renderCredentialsExpiredModal() { const { isSessionExpiredModalOpen } = this.state return { this.handleSessionExpiredModalToggle() this.props.signOut() }}> Your session has expired.   } renderModal() { const { user, tenant, timezone } = this.props const { isModalOpen, showZuulClientConfig } = this.state let config = this.ZuulClientConfig(tenant, user.data) let valid_until = moment.unix(user.data.expires_at).tz(timezone).format('YYYY-MM-DD HH:mm:ss') return ( { this.props.signOut() }} title="Note that you will be logged out of Zuul, but not out of your identity provider."> Sign Out   ]} >

Name: {user.data.profile.name}

Logged in as: {user.data.profile.preferred_username}  {(user.isAdmin && user.scope.indexOf(tenant.name) !== -1) && ( )}

Show Zuul Client Config {config}

Token expiry date: {valid_until}

Zuul stores and uses information such as your username and your email to provide some features. This data is stored in your browser only and is discarded once you log out.

) } renderButton(containerStyles) { const { user } = this.props if (!user.data) { return (
) } else { return (user.data.isFetching ?
Loading...
:
{this.renderModal()} {this.renderCredentialsExpiredModal()}
) } } render() { const { info, auth } = this.props const textColor = '#d1d1d1' const containerStyles = { color: textColor, border: 'solid #2b2b2b', borderWidth: '0 0 0 1px', display: 'initial', padding: '6px' } if (info.isFetching) { return (<>
Fetching auth info ...
) } // auth_params.authority is only set if an OpenID Connect auth is available if (auth.info && auth.info.default_realm && auth.auth_params.authority) { return this.renderButton(containerStyles) } else { return (
-
) } } } export default connect(state => ({ auth: state.auth, user: state.user, tenant: state.tenant, timezone: state.timezone, info: state.info, }))(withAuth(AuthContainer))