fix(oauth-providers): OAuth Providers Github App email problem (#421)

* [UPDATE (OAuth Providers): Github] request emails if Github App

* changlog

* [FIX: changeset] minor bump instead patch bump
pull/425/head
Carlos Sanjines Aldazosa 2024-03-20 01:37:16 -04:00 committed by GitHub
parent 830559c559
commit cef4be898a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/oauth-providers': patch
---
Github App user email problem

View File

@ -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
@ -15,6 +21,9 @@ type Token = {
token: string
expires_in?: number
}
const userAgent = 'Hono-Auth-App'
export class AuthFlow {
client_id: string
client_secret: string
@ -95,7 +104,7 @@ export class AuthFlow {
Authorization: `Bearer ${this.token?.token}`,
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'Hono-Auth-App',
'User-Agent': userAgent,
},
}).then((res) => res.json())) as GitHubUser | GitHubErrorResponse
@ -103,6 +112,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': userAgent,
},
}).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
}

View File

@ -95,3 +95,10 @@ export type GitHubUser = {
private_repos: number
}
}
export type GitHubEmailResponse = {
email: string
primary: boolean
vrified: boolean
visibility: string
}

View File

@ -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',
@ -265,7 +266,7 @@ export const githubUser = {
company: '@rvesoftware',
blog: 'https://monoald.github.io/',
location: 'Knowhere',
email: null,
email: 'test@email.com',
hireable: null,
bio: 'BIO description',
twitter_username: 'monoald',
@ -288,6 +289,20 @@ export const githubUser = {
private_repos: 10000,
},
}
export const githubEmails = [
{
email: 'test@email.com',
primary: true,
verified: true,
visibility: 'public',
},
{
email: '671450+test@users.noreply.github.com',
primary: false,
verified: true,
visibility: null,
},
]
export const githubCodeError = {
error_description: 'Invalid Code.',
}