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 bumppull/425/head
parent
830559c559
commit
cef4be898a
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/oauth-providers': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Github App user email problem
|
|
@ -1,7 +1,13 @@
|
||||||
import { HTTPException } from 'hono/http-exception'
|
import { HTTPException } from 'hono/http-exception'
|
||||||
|
|
||||||
import { toQueryParams } from '../../utils/objectToQuery'
|
import { toQueryParams } from '../../utils/objectToQuery'
|
||||||
import type { GitHubErrorResponse, GitHubTokenResponse, GitHubUser, GitHubScope } from './types'
|
import type {
|
||||||
|
GitHubErrorResponse,
|
||||||
|
GitHubTokenResponse,
|
||||||
|
GitHubUser,
|
||||||
|
GitHubScope,
|
||||||
|
GitHubEmailResponse,
|
||||||
|
} from './types'
|
||||||
|
|
||||||
type GithubAuthFlow = {
|
type GithubAuthFlow = {
|
||||||
client_id: string
|
client_id: string
|
||||||
|
@ -15,6 +21,9 @@ type Token = {
|
||||||
token: string
|
token: string
|
||||||
expires_in?: number
|
expires_in?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const userAgent = 'Hono-Auth-App'
|
||||||
|
|
||||||
export class AuthFlow {
|
export class AuthFlow {
|
||||||
client_id: string
|
client_id: string
|
||||||
client_secret: string
|
client_secret: string
|
||||||
|
@ -95,7 +104,7 @@ export class AuthFlow {
|
||||||
Authorization: `Bearer ${this.token?.token}`,
|
Authorization: `Bearer ${this.token?.token}`,
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'User-Agent': 'Hono-Auth-App',
|
'User-Agent': userAgent,
|
||||||
},
|
},
|
||||||
}).then((res) => res.json())) as GitHubUser | GitHubErrorResponse
|
}).then((res) => res.json())) as GitHubUser | GitHubErrorResponse
|
||||||
|
|
||||||
|
@ -103,6 +112,26 @@ export class AuthFlow {
|
||||||
throw new HTTPException(400, { message: response.message })
|
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) {
|
if ('id' in response) {
|
||||||
this.user = response
|
this.user = response
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,3 +95,10 @@ export type GitHubUser = {
|
||||||
private_repos: number
|
private_repos: number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type GitHubEmailResponse = {
|
||||||
|
email: string
|
||||||
|
primary: boolean
|
||||||
|
vrified: boolean
|
||||||
|
visibility: string
|
||||||
|
}
|
||||||
|
|
|
@ -81,6 +81,7 @@ export const handlers = [
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
http.get('https://api.github.com/user', () => HttpResponse.json(githubUser)),
|
http.get('https://api.github.com/user', () => HttpResponse.json(githubUser)),
|
||||||
|
http.get('https://api.github.com/user/emails', () => HttpResponse.json(githubEmails)),
|
||||||
// LinkedIn
|
// LinkedIn
|
||||||
http.post(
|
http.post(
|
||||||
'https://www.linkedin.com/oauth/v2/accessToken',
|
'https://www.linkedin.com/oauth/v2/accessToken',
|
||||||
|
@ -265,7 +266,7 @@ export const githubUser = {
|
||||||
company: '@rvesoftware',
|
company: '@rvesoftware',
|
||||||
blog: 'https://monoald.github.io/',
|
blog: 'https://monoald.github.io/',
|
||||||
location: 'Knowhere',
|
location: 'Knowhere',
|
||||||
email: null,
|
email: 'test@email.com',
|
||||||
hireable: null,
|
hireable: null,
|
||||||
bio: 'BIO description',
|
bio: 'BIO description',
|
||||||
twitter_username: 'monoald',
|
twitter_username: 'monoald',
|
||||||
|
@ -288,6 +289,20 @@ export const githubUser = {
|
||||||
private_repos: 10000,
|
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 = {
|
export const githubCodeError = {
|
||||||
error_description: 'Invalid Code.',
|
error_description: 'Invalid Code.',
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue