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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add window config string #552

Merged
merged 4 commits into from Apr 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/auth-client/src/index.test.ts
Expand Up @@ -217,17 +217,32 @@ describe('Auth Client login', () => {
const client = await AuthClient.create();
// Try without #authorize hash.
await client.login({ identityProvider: 'http://localhost' });
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow');
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow', undefined);

// Try with #authorize hash.
global.open = jest.fn();
await client.login({ identityProvider: 'http://localhost#authorize' });
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow');
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow', undefined);

// Default url
global.open = jest.fn();
await client.login();
expect(global.open).toBeCalledWith('https://identity.ic0.app/#authorize', 'idpWindow');
expect(global.open).toBeCalledWith(
'https://identity.ic0.app/#authorize',
'idpWindow',
undefined,
);

// Default custom window.open feature
global.open = jest.fn();
await client.login({
windowOpenerFeatures: 'toolbar=0,location=0,menubar=0',
});
expect(global.open).toBeCalledWith(
'https://identity.ic0.app/#authorize',
'idpWindow',
'toolbar=0,location=0,menubar=0',
);
});

it('should ignore authorize-ready events with bad origin', async () => {
Expand Down
19 changes: 16 additions & 3 deletions packages/auth-client/src/index.ts
@@ -1,6 +1,5 @@
/** @module AuthClient */
import {
Actor,
AnonymousIdentity,
DerEncodedPublicKey,
Identity,
Expand Down Expand Up @@ -64,6 +63,11 @@ export interface AuthClientLoginOptions {
* @default BigInt(8) hours * BigInt(3_600_000_000_000) nanoseconds
*/
maxTimeToLive?: bigint;
/**
* Auth Window feature config string
* @example "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100"
*/
windowOpenerFeatures?: string;
/**
* Callback once login has completed
*/
Expand Down Expand Up @@ -327,13 +331,15 @@ export class AuthClient {
* @param {AuthClientLoginOptions} options
* @param options.identityProvider Identity provider
* @param options.maxTimeToLive Expiration of the authentication in nanoseconds
* @param options.windowOpenerFeatures Configures the opened authentication window
* @param options.onSuccess Callback once login has completed
* @param options.onError Callback in case authentication fails
* @example
* const authClient = await AuthClient.create();
* authClient.login({
* identityProvider: 'http://<canisterID>.localhost:8000',
* maxTimeToLive: BigInt (7) * BigInt(24) * BigInt(3_600_000_000_000) // 1 week
* maxTimeToLive: BigInt (7) * BigInt(24) * BigInt(3_600_000_000_000), // 1 week
* windowOpenerFeatures: "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100",
* onSuccess: () => {
* console.log('Login Successful!');
* },
Expand All @@ -353,6 +359,11 @@ export class AuthClient {
* @default BigInt(8) hours * BigInt(3_600_000_000_000) nanoseconds
*/
maxTimeToLive?: bigint;
/**
* Auth Window feature config string
* @example "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100"
*/
windowOpenerFeatures?: string;
/**
* Callback once login has completed
*/
Expand Down Expand Up @@ -393,7 +404,9 @@ export class AuthClient {
window.addEventListener('message', this._eventHandler);

// Open a new window with the IDP provider.
this._idpWindow = window.open(identityProviderUrl.toString(), 'idpWindow') ?? undefined;
this._idpWindow =
window.open(identityProviderUrl.toString(), 'idpWindow', options?.windowOpenerFeatures) ??
undefined;

// Check if the _idpWindow is closed by user.
const checkInterruption = (): void => {
Expand Down