Skip to content

Commit

Permalink
no need to send state when requesting oauth token for user
Browse files Browse the repository at this point in the history
  • Loading branch information
outofambit committed Sep 12, 2018
1 parent eb9dbe1 commit da87502
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
2 changes: 0 additions & 2 deletions app/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,6 @@ export function getOAuthAuthorizationURL(

export async function requestOAuthToken(
endpoint: string,
state: string,
code: string
): Promise<string | null> {
try {
Expand All @@ -879,7 +878,6 @@ export async function requestOAuthToken(
client_id: ClientID,
client_secret: ClientSecret,
code: code,
state: state,
}
)
const result = await parsedResponse<IAPIAccessToken>(response)
Expand Down
2 changes: 1 addition & 1 deletion app/src/lib/dispatcher/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ export class Dispatcher {
case 'oauth':
try {
log.info(`[Dispatcher] requesting authenticated user`)
const user = await requestAuthenticatedUser(action.code)
const user = await requestAuthenticatedUser(action.code, action.state)
if (user) {
resolveOAuthRequest(user)
} else if (user === null) {
Expand Down
11 changes: 4 additions & 7 deletions app/src/lib/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,17 @@ export function askUserToOAuth(endpoint: string) {
* the code cannot be used to retrieve a valid GitHub user.
*/
export async function requestAuthenticatedUser(
code: string
code: string,
state: string
): Promise<Account | null | undefined> {
if (!oauthState) {
if (!oauthState || state !== oauthState.state) {
log.warn(
'requestAuthenticatedUser was not called with valid OAuth state. This is likely due to a browser reloading the callback URL. Contact GitHub Support if you believe this is an error'
)
return undefined
}

const token = await requestOAuthToken(
oauthState.endpoint,
oauthState.state,
code
)
const token = await requestOAuthToken(oauthState.endpoint, code)
if (token) {
return fetchUser(oauthState.endpoint, token)
} else {
Expand Down
6 changes: 4 additions & 2 deletions app/src/lib/parse-app-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { testForInvalidChars } from './sanitize-branch'
export interface IOAuthAction {
readonly name: 'oauth'
readonly code: string
readonly state: string
}

export interface IOpenRepositoryFromURLAction {
Expand Down Expand Up @@ -83,8 +84,9 @@ export function parseAppURL(url: string): URLActionType {
const actionName = hostname.toLowerCase()
if (actionName === 'oauth') {
const code = getQueryStringValue(query, 'code')
if (code != null) {
return { name: 'oauth', code }
const state = getQueryStringValue(query, 'state')
if (code != null && state != null) {
return { name: 'oauth', code, state }
} else {
return unknown
}
Expand Down

0 comments on commit da87502

Please sign in to comment.