Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: oauth auth flow #210

Merged
merged 5 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cognito-auth/src/__mocks__/cognito-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const mockCognitoUserSession = {
getJwtToken: () => 'MOCK_ID_TOKEN',
getExpiration: () => 1
} as any),
isValid: () => true
isValid: () => true,
} as CognitoUserSession

export const mockLoginSession: LoginSession = {
Expand All @@ -28,6 +28,7 @@ export const mockLoginSession: LoginSession = {
idToken: 'MOCK_ID_TOKEN',
idTokenExpiry: 2,
mode: 'WEB',
cognitoClientId: 'SOME_CLIENT_ID',
loginIdentity: {
email: 'bob@acme.com',
developerId: 'SOME_DEV_ID',
Expand Down
1 change: 1 addition & 0 deletions packages/cognito-auth/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface LoginSession {
loginType: LoginType
loginIdentity: LoginIdentity
mode: LoginMode
cognitoClientId: string
}

export interface ChangePasswordParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ jest.mock('@reapit/elements', () => ({

describe('tokenRefreshUserSessionService', () => {
it('should return a LoginSession', async () => {
expect(await tokenRefreshUserSessionService('bob@acme.com', 'MOCK_REFRESH_TOKEN', 'someCognitoClientId')).toEqual(
getLoginSession(mockCognitoUserSession),
)
expect(await tokenRefreshUserSessionService('bob@acme.com', 'MOCK_REFRESH_TOKEN', 'someCognitoClientId')).toEqual({
...getLoginSession(mockCognitoUserSession),
cognitoClientId: 'someCognitoClientId',
})
})
})

Expand All @@ -28,6 +29,7 @@ describe('codeRefreshUserSessionService', () => {
idToken: 'SOME_TOKEN',
idTokenExpiry: 1570750731,
refreshToken: 'SOME_TOKEN',
cognitoClientId: 'someCognitoClientId',
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const ONE_MINUTE_SECS = 60
export const tokenRefreshUserSessionService = async (
userName: string,
refreshToken: string,
congitoClientId: string,
cognitoClientId: string,
): Promise<Partial<LoginSession>> => {
return new Promise((resolve, reject) => {
const refreshTokenObject = {
getToken: () => refreshToken,
}
const cognitoUser = getNewUser(userName, congitoClientId)
const cognitoUser = getNewUser(userName, cognitoClientId)

cognitoUser.refreshSession(refreshTokenObject, (err, session) => {
if (!err && session) {
return resolve(getLoginSession(session))
return resolve({ ...getLoginSession(session), cognitoClientId })
}
return reject(`${errorStrings.TOKEN_REFRESH_SESSION_SERVICE_ERROR} ${JSON.stringify(err)}`)
})
Expand All @@ -29,14 +29,14 @@ export const tokenRefreshUserSessionService = async (
export const codeRefreshUserSessionService = async (
authorizationCode: string,
redirectUri: string,
congitoClientId: string,
cognitoClientId: string,
loginType: LoginType = 'CLIENT',
): Promise<Partial<LoginSession>> => {
const session = await fetcher({
method: 'POST',
api: process.env.COGNITO_OAUTH_URL as string,
url:
`/token?grant_type=authorization_code&client_id=${congitoClientId}` +
`/token?grant_type=authorization_code&client_id=${cognitoClientId}` +
`&code=${authorizationCode}&redirect_uri=${redirectUri}&state=${loginType}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Expand All @@ -53,6 +53,7 @@ export const codeRefreshUserSessionService = async (
idToken: id_token,
idTokenExpiry: tokenExpiry,
refreshToken: refresh_token,
cognitoClientId,
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/cognito-auth/src/utils/cognito.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Session utils', () => {
loginType: mockLoginSession.loginType,
userName: mockLoginSession.userName,
mode: 'WEB',
cognitoClientId: 'SOME_CLIENT_ID',
}),
{
path: '/',
Expand Down
3 changes: 2 additions & 1 deletion packages/cognito-auth/src/utils/cognito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ export const getNewUser = (userName: string, cognitoClientId: string) => {
}

export const setSessionCookie = (session: LoginSession, identifier: string = COOKIE_SESSION_KEY): void => {
const { userName, refreshToken, loginType, mode } = session
const { userName, refreshToken, loginType, mode, cognitoClientId } = session
hardtack.set(
identifier,
JSON.stringify({
refreshToken,
loginType,
userName,
mode,
cognitoClientId,
}),
{
path: '/',
Expand Down
1 change: 1 addition & 0 deletions packages/marketplace/src/core/__mocks__/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const mockLoginSession: LoginSession = {
accessToken: 'MOCK_ACCESS_TOKEN',
idToken: 'MOCK_ID_TOKEN',
idTokenExpiry: 2,
cognitoClientId: 'SOME_CLIENT_ID',
mode: 'WEB',
loginIdentity: {
email: 'bob@acme.com',
Expand Down
2 changes: 1 addition & 1 deletion packages/marketplace/src/reducers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const defaultState = (): AuthState => {
error: false,
loginSession: null,
firstLogin: false,
loginType: 'CLIENT',
loginType: refreshSession ? refreshSession.loginType : 'CLIENT',
refreshSession,
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.