Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPMRPP-89400 || Set bearer token in api doc #3793

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/controllers/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export { authReducer } from './reducer';
export {
isAuthorizedSelector,
tokenSelector,
tokenTypeSelector,
tokenValueSelector,
lastFailedLoginTimeSelector,
badCredentialsSelector,
} from './selectors';
Expand Down
4 changes: 2 additions & 2 deletions app/src/controllers/auth/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const authSelector = (state) => state.auth || {};
export const isAuthorizedSelector = (state) => !!authSelector(state).authorized;

const userTokenSelector = (state) => authSelector(state).token;
const tokenTypeSelector = (state) => userTokenSelector(state).type;
const tokenValueSelector = (state) => userTokenSelector(state).value;
export const tokenTypeSelector = (state) => userTokenSelector(state).type;
export const tokenValueSelector = (state) => userTokenSelector(state).value;
const tokenStringSelector = (state) => `${tokenTypeSelector(state)} ${tokenValueSelector(state)}`;

export const tokenSelector = (state) => tokenStringSelector(state);
Expand Down
18 changes: 14 additions & 4 deletions app/src/pages/inside/apiPage/apiPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import PropTypes from 'prop-types';
import SwaggerUI from 'swagger-ui-react';
import 'swagger-ui-react/swagger-ui.css';
import { URLS, DEFAULT_API_URL_PREFIX, UAT_API_URL_PREFIX } from 'common/urls';
import { tokenSelector } from 'controllers/auth';
import { tokenTypeSelector, tokenValueSelector } from 'controllers/auth';
import { PageLayout, PageHeader, PageSection } from 'layouts/pageLayout';
import { ToggleButton } from 'components/buttons/toggleButton';
import track from 'react-tracking';
Expand All @@ -43,16 +43,19 @@ const OPTION_BLOCK_TAG_SECTION_OPEN = '.opblock-tag-section.is-open';
const OPTION_BLOCK_TAG = '.opblock-tag';
const OPTION_BLOCK_TAG_OPEN = '.opblock.is-open';
const OPTION_BLOCK_SUMMARY = '.opblock-summary';
const SECURITY_SCHEMES_BEARER_NAME = 'bearerAuth';

@connect((state) => ({
token: tokenSelector(state),
tokenType: tokenTypeSelector(state),
tokenValue: tokenValueSelector(state),
}))
@injectIntl
@track()
export class ApiPage extends Component {
static propTypes = {
intl: PropTypes.object.isRequired,
token: PropTypes.string.isRequired,
tokenType: PropTypes.string.isRequired,
tokenValue: PropTypes.string.isRequired,
tracking: PropTypes.shape({
trackEvent: PropTypes.func,
getTrackingData: PropTypes.func,
Expand All @@ -63,6 +66,10 @@ export class ApiPage extends Component {
apiType: DEFAULT_API_URL_PREFIX,
};

onSwaggerRendering = (system) => {
system.preauthorizeApiKey(SECURITY_SCHEMES_BEARER_NAME, this.props.tokenValue);
};

getBreadcrumbs = () => [{ title: this.props.intl.formatMessage(messages.apiPageTitle) }];

getAPIName = (apiType) => apiType.split('/')[1];
Expand All @@ -78,7 +85,9 @@ export class ApiPage extends Component {
};

setAuth = (request) => {
request.headers.Authorization = this.props.token;
if (!request.headers.Authorization) {
request.headers.Authorization = `${this.props.tokenType} ${this.props.tokenValue}`;
}
return request;
};

Expand Down Expand Up @@ -178,6 +187,7 @@ export class ApiPage extends Component {
'head',
'options',
]}
onComplete={this.onSwaggerRendering}
/>
</div>
</div>
Expand Down
Loading