This commit is contained in:
74
node_modules/decap-cms-backend-aws-cognito-github-proxy/src/AuthenticationPage.js
generated
vendored
Normal file
74
node_modules/decap-cms-backend-aws-cognito-github-proxy/src/AuthenticationPage.js
generated
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import styled from '@emotion/styled';
|
||||
import { PkceAuthenticator } from 'decap-cms-lib-auth';
|
||||
import { AuthenticationPage, Icon } from 'decap-cms-ui-default';
|
||||
|
||||
const LoginButtonIcon = styled(Icon)`
|
||||
margin-right: 18px;
|
||||
`;
|
||||
|
||||
export default class GenericPKCEAuthenticationPage extends React.Component {
|
||||
static propTypes = {
|
||||
inProgress: PropTypes.bool,
|
||||
config: PropTypes.object.isRequired,
|
||||
onLogin: PropTypes.func.isRequired,
|
||||
t: PropTypes.func.isRequired,
|
||||
};
|
||||
|
||||
state = {};
|
||||
|
||||
componentDidMount() {
|
||||
const {
|
||||
base_url = '',
|
||||
app_id = '',
|
||||
auth_endpoint = 'oauth2/authorize',
|
||||
auth_token_endpoint = 'oauth2/token',
|
||||
} = this.props.config.backend;
|
||||
this.auth = new PkceAuthenticator({
|
||||
base_url,
|
||||
auth_endpoint,
|
||||
app_id,
|
||||
auth_token_endpoint,
|
||||
auth_token_endpoint_content_type: 'application/x-www-form-urlencoded; charset=utf-8',
|
||||
});
|
||||
// Complete authentication if we were redirected back to from the provider.
|
||||
this.auth.completeAuth((err, data) => {
|
||||
if (err) {
|
||||
this.setState({ loginError: err.toString() });
|
||||
return;
|
||||
}
|
||||
this.props.onLogin(data);
|
||||
});
|
||||
}
|
||||
|
||||
handleLogin = e => {
|
||||
e.preventDefault();
|
||||
this.auth.authenticate({ scope: 'https://api.github.com/repo openid email' }, (err, data) => {
|
||||
if (err) {
|
||||
this.setState({ loginError: err.toString() });
|
||||
return;
|
||||
}
|
||||
this.props.onLogin(data);
|
||||
});
|
||||
};
|
||||
|
||||
render() {
|
||||
const { inProgress, config, t } = this.props;
|
||||
return (
|
||||
<AuthenticationPage
|
||||
onLogin={this.handleLogin}
|
||||
loginDisabled={inProgress}
|
||||
loginErrorMessage={this.state.loginError}
|
||||
logoUrl={config.logo_url}
|
||||
siteUrl={config.site_url}
|
||||
renderButtonContent={() => (
|
||||
<React.Fragment>
|
||||
<LoginButtonIcon type="link" /> {inProgress ? t('auth.loggingIn') : t('auth.login')}
|
||||
</React.Fragment>
|
||||
)}
|
||||
t={t}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
52
node_modules/decap-cms-backend-aws-cognito-github-proxy/src/implementation.tsx
generated
vendored
Normal file
52
node_modules/decap-cms-backend-aws-cognito-github-proxy/src/implementation.tsx
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
import * as React from 'react';
|
||||
import { GitHubBackend } from 'decap-cms-backend-github';
|
||||
|
||||
import AuthenticationPage from './AuthenticationPage';
|
||||
|
||||
import type { GitHubUser } from 'decap-cms-backend-github/src/implementation';
|
||||
import type { Config } from 'decap-cms-lib-util/src';
|
||||
import type { Octokit } from '@octokit/rest';
|
||||
|
||||
export default class AwsCognitoGitHubProxyBackend extends GitHubBackend {
|
||||
constructor(config: Config, options = {}) {
|
||||
super(config, options);
|
||||
|
||||
this.bypassWriteAccessCheckForAppTokens = true;
|
||||
this.tokenKeyword = 'Bearer';
|
||||
}
|
||||
|
||||
authComponent() {
|
||||
const wrappedAuthenticationPage = (props: Record<string, unknown>) => (
|
||||
<AuthenticationPage {...props} backend={this} />
|
||||
);
|
||||
wrappedAuthenticationPage.displayName = 'AuthenticationPage';
|
||||
return wrappedAuthenticationPage;
|
||||
}
|
||||
|
||||
async currentUser({ token }: { token: string }): Promise<GitHubUser> {
|
||||
if (!this._currentUserPromise) {
|
||||
this._currentUserPromise = fetch(this.baseUrl + '/oauth2/userInfo', {
|
||||
headers: {
|
||||
Authorization: `${this.tokenKeyword} ${token}`,
|
||||
},
|
||||
}).then(async (res: Response): Promise<GitHubUser> => {
|
||||
if (res.status == 401) {
|
||||
this.logout();
|
||||
return Promise.reject('Token expired');
|
||||
}
|
||||
const userInfo = await res.json();
|
||||
const owner = this.originRepo.split('/')[1];
|
||||
return {
|
||||
name: userInfo.email,
|
||||
login: owner,
|
||||
avatar_url: `https://github.com/${owner}.png`,
|
||||
} as GitHubUser;
|
||||
});
|
||||
}
|
||||
return this._currentUserPromise;
|
||||
}
|
||||
|
||||
async getPullRequestAuthor(pullRequest: Octokit.PullsListResponseItem) {
|
||||
return pullRequest.user?.login;
|
||||
}
|
||||
}
|
||||
12
node_modules/decap-cms-backend-aws-cognito-github-proxy/src/index.ts
generated
vendored
Normal file
12
node_modules/decap-cms-backend-aws-cognito-github-proxy/src/index.ts
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import { API } from 'decap-cms-backend-github';
|
||||
|
||||
import AwsCognitoGitHubProxyBackend from './implementation';
|
||||
import AuthenticationPage from './AuthenticationPage';
|
||||
|
||||
export const DecapCmsBackendAwsCognitoGithubProxy = {
|
||||
AwsCognitoGitHubProxyBackend,
|
||||
API,
|
||||
AuthenticationPage,
|
||||
};
|
||||
|
||||
export { AwsCognitoGitHubProxyBackend, API, AuthenticationPage };
|
||||
Reference in New Issue
Block a user