Skip to content

Commit

Permalink
[UPDATE (OAuth Providers): Github] request emails if Github App
Browse files Browse the repository at this point in the history
  • Loading branch information
monoald committed Mar 14, 2024
1 parent 74dfa07 commit 1f4d23c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
28 changes: 27 additions & 1 deletion packages/oauth-providers/src/providers/github/authFlow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { HTTPException } from 'hono/http-exception'

import { toQueryParams } from '../../utils/objectToQuery'
import type { GitHubErrorResponse, GitHubTokenResponse, GitHubUser, GitHubScope } from './types'
import type {
GitHubErrorResponse,
GitHubTokenResponse,
GitHubUser,
GitHubScope,
GitHubEmailResponse,
} from './types'

type GithubAuthFlow = {
client_id: string
Expand Down Expand Up @@ -103,6 +109,26 @@ export class AuthFlow {
throw new HTTPException(400, { message: response.message })
}

if (!this.oauthApp) {
const emails = (await fetch('https://api.github.com/user/emails', {
headers: {
Authorization: `Bearer ${this.token?.token}`,
'User-Agent': 'Hono-Auth-App',
},
}).then((res) => res.json())) as GitHubEmailResponse[] | GitHubErrorResponse

if ('message' in emails) {
throw new HTTPException(400, { message: emails.message })
}

let email = emails.find((emails) => emails.primary === true)?.email
if (email === undefined) {
email = emails.find((emails) => !emails.email.includes('@users.noreply.github.com'))?.email
}

response.email = email as string
}

if ('id' in response) {
this.user = response
}
Expand Down
7 changes: 7 additions & 0 deletions packages/oauth-providers/src/providers/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ export type GitHubUser = {
private_repos: number
}
}

export type GitHubEmailResponse = {
email: string
primary: boolean
vrified: boolean
visibility: string
}
17 changes: 16 additions & 1 deletion packages/oauth-providers/test/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const handlers = [
}
),
http.get('https://api.github.com/user', () => HttpResponse.json(githubUser)),
http.get('https://api.github.com/user/emails', () => HttpResponse.json(githubEmails)),
// LinkedIn
http.post(
'https://www.linkedin.com/oauth/v2/accessToken',
Expand Down Expand Up @@ -265,7 +266,7 @@ export const githubUser = {
company: '@rvesoftware',
blog: 'https://monoald.github.io/',
location: 'Knowhere',
email: null,
email: '[email protected]',
hireable: null,
bio: 'BIO description',
twitter_username: 'monoald',
Expand All @@ -288,6 +289,20 @@ export const githubUser = {
private_repos: 10000,
},
}
export const githubEmails = [
{
email: '[email protected]',
primary: true,
verified: true,
visibility: 'public',
},
{
email: '[email protected]',
primary: false,
verified: true,
visibility: null,
},
]
export const githubCodeError = {
error_description: 'Invalid Code.',
}
Expand Down

0 comments on commit 1f4d23c

Please sign in to comment.