-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browser): sign-in session storage
- Loading branch information
Showing
7 changed files
with
163 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NormalizeKeyPaths } from '@silverhand/essentials'; | ||
import get from 'lodash.get'; | ||
|
||
const logtoClientErrorCodes = Object.freeze({ | ||
sign_in_session: { | ||
not_found: 'Sign-in session not found.', | ||
}, | ||
}); | ||
|
||
export type LogtoClientErrorCode = NormalizeKeyPaths<typeof logtoClientErrorCodes>; | ||
|
||
const getMessageByErrorCode = (errorCode: LogtoClientErrorCode): string => { | ||
// TODO: linear issue LOG-1419 | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const message = get(logtoClientErrorCodes, errorCode); | ||
|
||
if (typeof message === 'string') { | ||
return message; | ||
} | ||
|
||
return errorCode; | ||
}; | ||
|
||
export class LogtoClientError extends Error { | ||
code: LogtoClientErrorCode; | ||
data: unknown; | ||
|
||
constructor(code: LogtoClientErrorCode, data?: unknown) { | ||
super(getMessageByErrorCode(code)); | ||
this.code = code; | ||
this.data = data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,96 @@ | ||
import LogtoClient from './index'; | ||
import { LogtoClientError } from './errors'; | ||
import LogtoClient, { LogtoSignInSessionItem } from './index'; | ||
|
||
const clientId = 'client_id_value'; | ||
const endpoint = 'https://logto.dev'; | ||
const authorizationEndpoint = `${endpoint}/oidc/auth`; | ||
const mockedCodeVerifier = 'code_verifier_value'; | ||
const mockedState = 'state_value'; | ||
const mockedSignInUri = `${authorizationEndpoint}?foo=bar`; | ||
const redirectUri = 'http://localhost:3000/callback'; | ||
const requester = jest.fn(); | ||
const signInUri = `${authorizationEndpoint}?foo=bar`; | ||
|
||
jest.mock('@logto/js', () => { | ||
return { | ||
...jest.requireActual('@logto/js'), | ||
fetchOidcConfig: async () => ({ authorizationEndpoint }), | ||
generateSignInUri: () => signInUri, | ||
}; | ||
}); | ||
|
||
jest.mock('@logto/js', () => ({ | ||
...jest.requireActual('@logto/js'), | ||
fetchOidcConfig: async () => ({ authorizationEndpoint }), | ||
generateCodeChallenge: jest.fn().mockResolvedValue('code_challenge_value'), | ||
generateCodeVerifier: jest.fn(() => mockedCodeVerifier), | ||
generateSignInUri: jest.fn(() => mockedSignInUri), | ||
generateState: jest.fn(() => mockedState), | ||
})); | ||
|
||
/** | ||
* Make LogtoClient.signInSession accessible for test | ||
*/ | ||
class LogtoClientSignInSessionAccessor extends LogtoClient { | ||
public getSignInSessionItem(): LogtoSignInSessionItem { | ||
return this.signInSession; | ||
} | ||
|
||
public setSignInSessionItem(item: LogtoSignInSessionItem) { | ||
this.signInSession = item; | ||
} | ||
} | ||
|
||
describe('LogtoClient', () => { | ||
test('constructor', () => { | ||
expect(() => new LogtoClient({ endpoint, clientId, requester })).not.toThrow(); | ||
}); | ||
|
||
describe('signInSession', () => { | ||
test('getter should tHrow LogtoClientError when signInSession has not been set', () => { | ||
const signInSessionAccessor = new LogtoClientSignInSessionAccessor({ | ||
endpoint, | ||
clientId, | ||
requester, | ||
}); | ||
|
||
expect(() => signInSessionAccessor.getSignInSessionItem()).toMatchError( | ||
new LogtoClientError('sign_in_session.not_found') | ||
); | ||
}); | ||
|
||
test('getter should throw StructError when signInSession does not contain the required property', () => { | ||
const signInSessionAccessor = new LogtoClientSignInSessionAccessor({ | ||
endpoint, | ||
clientId, | ||
requester, | ||
}); | ||
|
||
// @ts-expect-error | ||
signInSessionAccessor.setSignInSessionItem({ | ||
redirectUri, | ||
codeVerifier: mockedCodeVerifier, | ||
}); | ||
|
||
expect(() => signInSessionAccessor.getSignInSessionItem()).toThrow( | ||
'At path: state -- Expected a string, but received: undefined' | ||
); | ||
}); | ||
|
||
test('should be able to set and get the correct item', () => { | ||
const signInSessionAccessor = new LogtoClientSignInSessionAccessor({ | ||
endpoint, | ||
clientId, | ||
requester, | ||
}); | ||
|
||
const logtoSignInSessionItem: LogtoSignInSessionItem = { | ||
redirectUri, | ||
codeVerifier: mockedCodeVerifier, | ||
state: mockedState, | ||
}; | ||
|
||
signInSessionAccessor.setSignInSessionItem(logtoSignInSessionItem); | ||
expect(signInSessionAccessor.getSignInSessionItem()).toEqual(logtoSignInSessionItem); | ||
}); | ||
}); | ||
|
||
describe('signIn', () => { | ||
test('window.location should be correct signInUri', async () => { | ||
const logtoClient = new LogtoClient({ endpoint, clientId, requester }); | ||
await logtoClient.signIn(redirectUri); | ||
expect(window.location.toString()).toEqual(signInUri); | ||
expect(window.location.toString()).toEqual(mockedSignInUri); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.