// 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 Name: {user.data.profile.name} Logged in as: {user.data.profile.preferred_username}
{(user.isAdmin && user.scope.indexOf(tenant.name) !== -1) && (
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.