-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into hour-format
- Loading branch information
Showing
16 changed files
with
263 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/decap-cms-backend-aws-cognito-github-proxy/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# GitHub backend | ||
|
||
An abstraction layer between the CMS and a proxied version of [Github](https://docs.github.com/en/rest). | ||
|
||
## Code structure | ||
|
||
`Implementation` - wraps [Github Backend](https://github.com/decaporg/decap-cms/tree/master/packages/decap-cms-lib-auth/README.md) for proxied version of Github. | ||
|
||
`AuthenticationPage` - uses [lib-auth](https://github.com/decaporg/decap-cms/tree/master/packages/decap-cms-lib-auth/README.md) to create an AWS Cognito compatible generic Authentication page supporting PKCE. |
45 changes: 45 additions & 0 deletions
45
packages/decap-cms-backend-aws-cognito-github-proxy/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "decap-cms-backend-aws-cognito-github-proxy", | ||
"description": "GitHub backend for Decap CMS proxied through AWS Cognito", | ||
"version": "3.1.0-beta.1", | ||
"license": "MIT", | ||
"repository": "https://github.com/decaporg/decap-cms/tree/master/packages/decap-cms-backend-aws-cognito-github-proxy", | ||
"bugs": "https://github.com/decaporg/decap-cms/issues", | ||
"module": "dist/esm/index.js", | ||
"main": "dist/decap-cms-backend-aws-cognito-github-proxy.js", | ||
"keywords": [ | ||
"decap-cms", | ||
"backend", | ||
"github", | ||
"aws-cognito" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
"develop": "yarn build:esm --watch", | ||
"build": "cross-env NODE_ENV=production webpack", | ||
"build:esm": "cross-env NODE_ENV=esm babel src --out-dir dist/esm --ignore \"**/__tests__\" --root-mode upward --extensions \".js,.jsx,.ts,.tsx\"", | ||
"createFragmentTypes": "node scripts/createFragmentTypes.js" | ||
}, | ||
"dependencies": { | ||
"apollo-cache-inmemory": "^1.6.2", | ||
"apollo-client": "^2.6.3", | ||
"apollo-link-context": "^1.0.18", | ||
"apollo-link-http": "^1.5.15", | ||
"common-tags": "^1.8.0", | ||
"graphql": "^15.0.0", | ||
"graphql-tag": "^2.10.1", | ||
"js-base64": "^3.0.0", | ||
"semaphore": "^1.1.0" | ||
}, | ||
"peerDependencies": { | ||
"@emotion/react": "^11.11.1", | ||
"@emotion/styled": "^11.11.0", | ||
"decap-cms-lib-auth": "^3.0.0", | ||
"decap-cms-backend-github": "^3.0.0", | ||
"decap-cms-lib-util": "^3.0.0", | ||
"decap-cms-ui-default": "^3.0.0", | ||
"lodash": "^4.17.11", | ||
"prop-types": "^15.7.2", | ||
"react": "^18.2.0" | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/decap-cms-backend-aws-cognito-github-proxy/src/AuthenticationPage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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', | ||
redirect_uri = document.location.origin + document.location.pathname, | ||
} = this.props.config.backend; | ||
this.auth = new PkceAuthenticator({ | ||
base_url, | ||
auth_endpoint, | ||
app_id, | ||
auth_token_endpoint, | ||
redirect_uri, | ||
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} | ||
/> | ||
); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/decap-cms-backend-aws-cognito-github-proxy/src/implementation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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'; | ||
|
||
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/decap-cms-backend-aws-cognito-github-proxy/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
3 changes: 3 additions & 0 deletions
3
packages/decap-cms-backend-aws-cognito-github-proxy/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { getConfig } = require('../../scripts/webpack.js'); | ||
|
||
module.exports = getConfig(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.