Skip to content

Commit

Permalink
use createOAuthAppAuth to create authentification when acting as the …
Browse files Browse the repository at this point in the history
…OAuthApp (#826)
  • Loading branch information
Shegox committed Jan 24, 2022
1 parent 48a2ddf commit 3c2b354
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/server/src/services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const logger = require('../services/logger')

const { Octokit } = require('@octokit/rest')
const { createAppAuth } = require('@octokit/auth-app')
const { createOAuthAppAuth } = require('@octokit/auth-oauth-app');

const OctokitWithPluginsAndDefaults = Octokit.plugin(
require('@octokit/plugin-retry').retry,
require('@octokit/plugin-throttling').throttling,
Expand Down Expand Up @@ -59,14 +61,20 @@ async function callGithub(octokit, obj, fun, arg, cacheKey, cacheTime) {

function determineAuthentication(token, basicAuth) {
if (token) {
return `token ${token}`
return { auth: `token ${token}` }
}
// basic Auth means that we want to act as the OAuthApp
if (basicAuth) {
return {
username: basicAuth.user,
password: basicAuth.pass
authStrategy: createOAuthAppAuth,
auth: {
clientId: basicAuth.user,
clientSecret: basicAuth.pass
}
}
}
// if nothing matches we return an empty object, so we have the same types
return {}
}

async function getInstallationId(octokit, arg) {
Expand Down Expand Up @@ -94,9 +102,9 @@ const githubService = {
const obj = call.obj
const cacheKey = generateCacheKey(arg, obj, fun, call.token)

const auth = determineAuthentication(call.token, call.basicAuth)
const { auth, authStrategy } = determineAuthentication(call.token, call.basicAuth)

const octokit = new OctokitWithPluginsAndDefaults({ auth })
const octokit = new OctokitWithPluginsAndDefaults({ auth, authStrategy })

if (!obj || !octokit[obj]) {
throw new Error(`${obj} required/object not found or specified`)
Expand Down
4 changes: 2 additions & 2 deletions src/tests/server/services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ describe('github:call', () => {

it('should authenticate when basic authentication is required', async () => {
expectedAuth = {
username: 'user',
password: 'pass'
clientId: 'user',
clientSecret: 'pass'
}
callStub.resolves({ data: {}, meta: {} })
await github.call({
Expand Down

0 comments on commit 3c2b354

Please sign in to comment.