Skip to content

Commit

Permalink
Add new GitHub auth to v3 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Jan 13, 2019
1 parent 2b10a1e commit 4e2f33b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
18 changes: 12 additions & 6 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,30 @@ const schema = {
default: 'https://github.com/login/oauth/access_token',
env: 'GITHUB_ACCESS_TOKEN_URI'
},
githubBaseUrl: {
doc: 'Base URL for the GitHub API.',
format: String,
default: 'https://api.github.com',
env: 'GITHUB_BASE_URL'
},
githubAppID: {
doc: 'ID of the GitHub App.',
format: String,
default: null,
env: 'GITHUB_APP_ID'
},
githubBaseUrl: {
doc: 'Base URL for the GitHub API.',
format: String,
default: 'https://api.github.com',
env: 'GITHUB_BASE_URL'
},
githubPrivateKey: {
doc: 'Private key for the GitHub App.',
format: String,
default: null,
env: 'GITHUB_PRIVATE_KEY'
},
githubToken: {
doc: 'Access token to the GitHub account (legacy)',
format: String,
default: null,
env: 'GITHUB_TOKEN'
},
gitlabAccessTokenUri: {
doc: 'URI for the GitLab authentication provider.',
format: String,
Expand Down
1 change: 1 addition & 0 deletions config.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"gitlabToken": "r4e3w2q1",
"githubToken": "1q2w3e4r",
"rsaPrivateKey": "-----BEGIN RSA PRIVATE KEY-----MIIEpAIBAAKCAQEA2nX81/5w6nZLolEh0uKks3//FlqizKxFWi8GaEJYbud8FYwLH6l+LDfZkjQiDXvm6mxngDGCjlG9b7hgfa/sfWSuqtJUQ2D1Nenn11gwAUaI7OQNSassE+nVFL2BGedl6DOgpZFkKrDTQT7jyvvc3r/2IqWahAYyFWXImD50qwNWIGyZ2Sry2/WXeCydiUWV8ZG3GOlGZLgtlK8igdmLje/6Ja+1oRBFwdbWrf/nKsMB0fCL3R5MkWTnumR8sGx2Xud8Q3mF7cOYWQiy2AqrFQOzJ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N1Yr6amM+8XqU1+UKLL2CBrLOSi6r4J4fPS8sb9362+FROwCJZfxd5Wznd/AoGBAN2NEz+anDBgulxLoguGuvc0FaqSsVIP/cdb5Ve5t29/uheVjLgDXSkV5P/RFfk1ygH1gNjmUrsoxfsGWQa3KMTk292maYRj7GPpOalSlp8BYyiQ9omPdOgusVzY4CEjMiDkKal0YrBswXKV8E2lTokEEkbdrQpToVdBmRCzT1ixAoGBAMNMBQ2CyO6ulEr75CyBU3O5QirhWE+uICFMNnvFNvd14VxYQgt9alcwL3jy+4QJYgJcLrWHRWfNU39Oe1MTOF+BVuIEnV7vdifNn7i+srd/nl7xOFHIG53DWMrSc5oQ8DIDo+LxCHqb0SHWY4pQ2qQ6JqQ1O/lPaFVvI5cxcevhAoGALyNtDQNgaAqsnCabe8hWz3INihVcFRHB9UDgMyIYnWiXt5ziK+TLVYqLBsL94eBH8tLBb2TSBXBPb3GST5N44SuwfCCEIt7/1OIymowv7/TjnCX7zpjvTtdgdVjPlz1d1RG2q49P0CnTnwW3801QwGZVXS6dOq1AjsguQRdlsoECgYBrFqKa3S48UbGOKkocgMb/1HzVs2sxNWMu+/jEiBvI4RrCiun9MALtrVtohgHRml4yZJaOojVk9F0z/MAak9eTgSmHpwxYZMqR1PvM4c/ekfun4hQoK04Thoexi/Z0RfsI6nBdBk8MAra03ldDvnYZY4Fat21YuNBspbrbTW8UoA==\n-----END RSA PRIVATE KEY-----",
"email": {
"apiKey": "key-1q2w3e4r"
},
Expand Down
3 changes: 2 additions & 1 deletion controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module.exports = (req, res) => {
.then(requestAccessToken)
.then((accessToken) => {
const git = gitFactory.create(req.params.service, {
oauthToken: accessToken
oauthToken: accessToken,
version: req.params.version
})

// TODO: Simplify this when v2 support is dropped.
Expand Down
14 changes: 13 additions & 1 deletion lib/GitHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ class GitHub extends GitService {
timeout: 5000
})

const isAppAuth = config.get('githubAppID') &&
config.get('githubPrivateKey')
const isLegacyAuth = config.get('githubToken') &&
['1', '2'].includes(options.version)

this.authentication = Promise.resolve()

if (options.oauthToken) {
this.api.authenticate({
type: 'oauth',
token: options.oauthToken
})
} else if (options.token) {
} else if (isLegacyAuth) {
this.api.authenticate({
type: 'token',
token: config.get('githubToken')
})
} else if (isAppAuth) {
this.authentication = this._authenticate(
options.username,
options.repository
Expand Down
6 changes: 4 additions & 2 deletions lib/GitLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ class GitLab extends GitService {
constructor (options = {}) {
super(options.username, options.repository, options.branch)

const token = config.get('gitlabToken')

if (options.oauthToken) {
this.api = new GitLabApi({
url: config.get('gitlabBaseUrl'),
oauthToken: options.oauthToken
})
} else if (options.token) {
} else if (token) {
this.api = new GitLabApi({
url: config.get('gitlabBaseUrl'),
token: options.token
token
})
} else {
throw new Error('Require an `oauthToken` or `token` option')
Expand Down
37 changes: 23 additions & 14 deletions lib/Staticman.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ const yaml = require('js-yaml')
const Staticman = function (parameters) {
this.parameters = parameters

const token = parameters.service === 'gitlab'
? config.get('gitlabToken')
: {
integrationId: config.get('githubAppID'),
privateKey: config.get('githubPrivateKey')
}
const {
branch,
repository,
service,
username,
version
} = parameters

// Initialise the Git service API
this.git = gitFactory.create(parameters.service, {
username: parameters.username,
repository: parameters.repository,
branch: parameters.branch,
token
})
this.git = gitFactory.create(service, {
branch,
repository,
username,
version
})

// Generate unique id
this.uid = uuidv1()
Expand Down Expand Up @@ -176,7 +177,10 @@ Staticman.prototype._checkAuth = function () {
return Promise.reject(errorHandler('AUTH_TOKEN_INVALID'))
}

const git = gitFactory.create(this.options['auth-type'], {oauthToken})
const git = gitFactory.create(this.options['auth-type'], {
oauthToken,
version: this.parameters.version
})

return git.getCurrentUser().then(user => {
this.gitUser = user
Expand All @@ -201,7 +205,10 @@ Staticman.prototype._checkAuthV2 = function () {
return Promise.reject(errorHandler('GITHUB_AUTH_TOKEN_INVALID'))
}

const git = gitFactory.create('github', {oauthToken})
const git = gitFactory.create('github', {
oauthToken,
version: this.parameters.version
})

return git.api.users.getAuthenticated({}).then(({data}) => {
this.gitUser = data
Expand Down Expand Up @@ -497,6 +504,8 @@ Staticman.prototype.processEntry = function (fields, options) {
this.fields = Object.assign({}, fields)
this.options = Object.assign({}, options)

this._initialiseGit

return this.getSiteConfig().then(config => {
return this._checkAuth()
}).then(() => {
Expand Down

0 comments on commit 4e2f33b

Please sign in to comment.