@dfinity/auth-client
Advanced tools
Comparing version 0.10.4 to 0.11.0
@@ -0,3 +1,5 @@ | ||
/** @module AuthClient */ | ||
import { Identity, SignIdentity } from '@dfinity/agent'; | ||
import { DelegationChain } from '@dfinity/identity'; | ||
import IdleManager, { IdleManagerOptions } from './idleManager'; | ||
export declare const ERROR_USER_INTERRUPT = "UserInterrupt"; | ||
@@ -16,6 +18,19 @@ /** | ||
storage?: AuthClientStorage; | ||
/** | ||
* Options to handle idle timeouts | ||
* @default after 30 minutes, invalidates the identity | ||
*/ | ||
idleOptions?: IdleOptions; | ||
} | ||
export interface IdleOptions extends IdleManagerOptions { | ||
/** | ||
* Disables idle functionality for {@link IdleManager} | ||
* @default false | ||
*/ | ||
disableIdle?: boolean; | ||
} | ||
export interface AuthClientLoginOptions { | ||
/** | ||
* Identity provider. By default, use the identity service. | ||
* Identity provider | ||
* @default "https://identity.ic0.app" | ||
*/ | ||
@@ -25,5 +40,11 @@ identityProvider?: string | URL; | ||
* Expiration of the authentication in nanoseconds | ||
* @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 | ||
@@ -54,2 +75,6 @@ */ | ||
} | ||
/** | ||
* Tool to manage authentication and identity | ||
* @see {@link AuthClient} | ||
*/ | ||
export declare class AuthClient { | ||
@@ -60,10 +85,92 @@ private _identity; | ||
private _storage; | ||
readonly idleManager: IdleManager | undefined; | ||
private _idpWindow?; | ||
private _eventHandler?; | ||
static create(options?: AuthClientCreateOptions): Promise<AuthClient>; | ||
protected constructor(_identity: Identity, _key: SignIdentity | null, _chain: DelegationChain | null, _storage: AuthClientStorage, _idpWindow?: Window | undefined, _eventHandler?: ((event: MessageEvent) => void) | undefined); | ||
/** | ||
* Create an AuthClient to manage authentication and identity | ||
* @constructs {@link AuthClient} | ||
* @param {AuthClientCreateOptions} options | ||
* @see {@link AuthClientCreateOptions} | ||
* @param options.identity Optional Identity to use as the base | ||
* @see {@link SignIdentity} | ||
* @param options.storage Storage mechanism for delegration credentials | ||
* @see {@link AuthClientStorage} | ||
* @param {IdleOptions} options.idleOptions Configures an {@link IdleManager} | ||
* @see {@link IdleOptions} | ||
* @example | ||
* const authClient = await AuthClient.create({ | ||
* idleOptions: { | ||
* disableIdle: true | ||
* } | ||
* }) | ||
*/ | ||
static create(options?: { | ||
/** | ||
* An {@link Identity} to use as the base. | ||
* By default, a new {@link AnonymousIdentity} | ||
*/ | ||
identity?: SignIdentity; | ||
/** | ||
* {@link AuthClientStorage} | ||
* @description Optional storage with get, set, and remove. Uses {@link LocalStorage} by default | ||
*/ | ||
storage?: AuthClientStorage; | ||
/** | ||
* Options to handle idle timeouts | ||
* @default after 10 minutes, invalidates the identity | ||
*/ | ||
idleOptions?: IdleOptions; | ||
}): Promise<AuthClient>; | ||
protected constructor(_identity: Identity, _key: SignIdentity | null, _chain: DelegationChain | null, _storage: AuthClientStorage, idleManager: IdleManager | undefined, _idpWindow?: Window | undefined, _eventHandler?: ((event: MessageEvent) => void) | undefined); | ||
private _handleSuccess; | ||
getIdentity(): Identity; | ||
isAuthenticated(): Promise<boolean>; | ||
login(options?: AuthClientLoginOptions): Promise<void>; | ||
/** | ||
* AuthClient Login - | ||
* Opens up a new window to authenticate with Internet Identity | ||
* @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 | ||
* windowOpenerFeatures: "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100", | ||
* onSuccess: () => { | ||
* console.log('Login Successful!'); | ||
* }, | ||
* onError: (error) => { | ||
* console.error('Login Failed: ', error); | ||
* } | ||
* }); | ||
*/ | ||
login(options?: { | ||
/** | ||
* Identity provider | ||
* @default "https://identity.ic0.app" | ||
*/ | ||
identityProvider?: string | URL; | ||
/** | ||
* Expiration of the authentication in nanoseconds | ||
* @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 | ||
*/ | ||
onSuccess?: (() => void) | (() => Promise<void>); | ||
/** | ||
* Callback in case authentication fails | ||
*/ | ||
onError?: ((error?: string) => void) | ((error?: string) => Promise<void>); | ||
}): Promise<void>; | ||
private _getEventHandler; | ||
@@ -70,0 +177,0 @@ private _handleFailure; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthClient = exports.LocalStorage = exports.ERROR_USER_INTERRUPT = void 0; | ||
/** @module AuthClient */ | ||
const agent_1 = require("@dfinity/agent"); | ||
const authentication_1 = require("@dfinity/authentication"); | ||
const identity_1 = require("@dfinity/identity"); | ||
const idleManager_1 = __importDefault(require("./idleManager")); | ||
const KEY_LOCALSTORAGE_KEY = 'identity'; | ||
@@ -51,4 +56,8 @@ const KEY_LOCALSTORAGE_DELEGATION = 'delegation'; | ||
exports.LocalStorage = LocalStorage; | ||
/** | ||
* Tool to manage authentication and identity | ||
* @see {@link AuthClient} | ||
*/ | ||
class AuthClient { | ||
constructor(_identity, _key, _chain, _storage, | ||
constructor(_identity, _key, _chain, _storage, idleManager, | ||
// A handle on the IdP window. | ||
@@ -58,2 +67,3 @@ _idpWindow, | ||
_eventHandler) { | ||
var _a; | ||
this._identity = _identity; | ||
@@ -63,7 +73,28 @@ this._key = _key; | ||
this._storage = _storage; | ||
this.idleManager = idleManager; | ||
this._idpWindow = _idpWindow; | ||
this._eventHandler = _eventHandler; | ||
const logout = this.logout.bind(this); | ||
(_a = this.idleManager) === null || _a === void 0 ? void 0 : _a.registerCallback(logout); | ||
} | ||
/** | ||
* Create an AuthClient to manage authentication and identity | ||
* @constructs {@link AuthClient} | ||
* @param {AuthClientCreateOptions} options | ||
* @see {@link AuthClientCreateOptions} | ||
* @param options.identity Optional Identity to use as the base | ||
* @see {@link SignIdentity} | ||
* @param options.storage Storage mechanism for delegration credentials | ||
* @see {@link AuthClientStorage} | ||
* @param {IdleOptions} options.idleOptions Configures an {@link IdleManager} | ||
* @see {@link IdleOptions} | ||
* @example | ||
* const authClient = await AuthClient.create({ | ||
* idleOptions: { | ||
* disableIdle: true | ||
* } | ||
* }) | ||
*/ | ||
static async create(options = {}) { | ||
var _a; | ||
var _a, _b; | ||
const storage = (_a = options.storage) !== null && _a !== void 0 ? _a : new LocalStorage('ic-'); | ||
@@ -113,3 +144,6 @@ let key = null; | ||
} | ||
return new this(identity, key, chain, storage); | ||
const idleManager = ((_b = options.idleOptions) === null || _b === void 0 ? void 0 : _b.disableIdle) | ||
? undefined | ||
: idleManager_1.default.create(options.idleOptions); | ||
return new this(identity, key, chain, storage, idleManager); | ||
} | ||
@@ -142,4 +176,27 @@ _handleSuccess(message, onSuccess) { | ||
} | ||
/** | ||
* AuthClient Login - | ||
* Opens up a new window to authenticate with Internet Identity | ||
* @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 | ||
* windowOpenerFeatures: "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100", | ||
* onSuccess: () => { | ||
* console.log('Login Successful!'); | ||
* }, | ||
* onError: (error) => { | ||
* console.error('Login Failed: ', error); | ||
* } | ||
* }); | ||
*/ | ||
async login(options) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d; | ||
let key = this._key; | ||
@@ -152,5 +209,4 @@ if (!key) { | ||
} | ||
// Set default maxTimeToLive to 1 day | ||
const defaultTimeToLive = | ||
/* days */ BigInt(1) * /* hours */ BigInt(24) * /* nanoseconds */ BigInt(3600000000000); | ||
// Set default maxTimeToLive to 8 hours | ||
const defaultTimeToLive = /* hours */ BigInt(8) * /* nanoseconds */ BigInt(3600000000000); | ||
// Create the URL of the IDP. (e.g. https://XXXX/#authorize) | ||
@@ -165,6 +221,7 @@ const identityProviderUrl = new URL(((_a = options === null || options === void 0 ? void 0 : options.identityProvider) === null || _a === void 0 ? void 0 : _a.toString()) || IDENTITY_PROVIDER_DEFAULT); | ||
// Add an event listener to handle responses. | ||
this._eventHandler = this._getEventHandler(identityProviderUrl, Object.assign({ maxTimeToLive: defaultTimeToLive }, options)); | ||
this._eventHandler = this._getEventHandler(identityProviderUrl, Object.assign({ maxTimeToLive: (_c = options === null || options === void 0 ? void 0 : options.maxTimeToLive) !== null && _c !== void 0 ? _c : defaultTimeToLive }, options)); | ||
window.addEventListener('message', this._eventHandler); | ||
// Open a new window with the IDP provider. | ||
this._idpWindow = (_c = window.open(identityProviderUrl.toString(), 'idpWindow')) !== null && _c !== void 0 ? _c : undefined; | ||
this._idpWindow = | ||
(_d = window.open(identityProviderUrl.toString(), 'idpWindow', options === null || options === void 0 ? void 0 : options.windowOpenerFeatures)) !== null && _d !== void 0 ? _d : undefined; | ||
// Check if the _idpWindow is closed by user. | ||
@@ -188,2 +245,3 @@ const checkInterruption = () => { | ||
if (event.origin !== identityProviderUrl.origin) { | ||
console.warn(`WARNING: expected origin '${identityProviderUrl.origin}', got '${event.origin}' (ignoring)`); | ||
return; | ||
@@ -190,0 +248,0 @@ } |
@@ -0,3 +1,5 @@ | ||
/** @module AuthClient */ | ||
import { Identity, SignIdentity } from '@dfinity/agent'; | ||
import { DelegationChain } from '@dfinity/identity'; | ||
import IdleManager, { IdleManagerOptions } from './idleManager'; | ||
export declare const ERROR_USER_INTERRUPT = "UserInterrupt"; | ||
@@ -16,6 +18,19 @@ /** | ||
storage?: AuthClientStorage; | ||
/** | ||
* Options to handle idle timeouts | ||
* @default after 30 minutes, invalidates the identity | ||
*/ | ||
idleOptions?: IdleOptions; | ||
} | ||
export interface IdleOptions extends IdleManagerOptions { | ||
/** | ||
* Disables idle functionality for {@link IdleManager} | ||
* @default false | ||
*/ | ||
disableIdle?: boolean; | ||
} | ||
export interface AuthClientLoginOptions { | ||
/** | ||
* Identity provider. By default, use the identity service. | ||
* Identity provider | ||
* @default "https://identity.ic0.app" | ||
*/ | ||
@@ -25,5 +40,11 @@ identityProvider?: string | URL; | ||
* Expiration of the authentication in nanoseconds | ||
* @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 | ||
@@ -54,2 +75,6 @@ */ | ||
} | ||
/** | ||
* Tool to manage authentication and identity | ||
* @see {@link AuthClient} | ||
*/ | ||
export declare class AuthClient { | ||
@@ -60,10 +85,92 @@ private _identity; | ||
private _storage; | ||
readonly idleManager: IdleManager | undefined; | ||
private _idpWindow?; | ||
private _eventHandler?; | ||
static create(options?: AuthClientCreateOptions): Promise<AuthClient>; | ||
protected constructor(_identity: Identity, _key: SignIdentity | null, _chain: DelegationChain | null, _storage: AuthClientStorage, _idpWindow?: Window | undefined, _eventHandler?: ((event: MessageEvent) => void) | undefined); | ||
/** | ||
* Create an AuthClient to manage authentication and identity | ||
* @constructs {@link AuthClient} | ||
* @param {AuthClientCreateOptions} options | ||
* @see {@link AuthClientCreateOptions} | ||
* @param options.identity Optional Identity to use as the base | ||
* @see {@link SignIdentity} | ||
* @param options.storage Storage mechanism for delegration credentials | ||
* @see {@link AuthClientStorage} | ||
* @param {IdleOptions} options.idleOptions Configures an {@link IdleManager} | ||
* @see {@link IdleOptions} | ||
* @example | ||
* const authClient = await AuthClient.create({ | ||
* idleOptions: { | ||
* disableIdle: true | ||
* } | ||
* }) | ||
*/ | ||
static create(options?: { | ||
/** | ||
* An {@link Identity} to use as the base. | ||
* By default, a new {@link AnonymousIdentity} | ||
*/ | ||
identity?: SignIdentity; | ||
/** | ||
* {@link AuthClientStorage} | ||
* @description Optional storage with get, set, and remove. Uses {@link LocalStorage} by default | ||
*/ | ||
storage?: AuthClientStorage; | ||
/** | ||
* Options to handle idle timeouts | ||
* @default after 10 minutes, invalidates the identity | ||
*/ | ||
idleOptions?: IdleOptions; | ||
}): Promise<AuthClient>; | ||
protected constructor(_identity: Identity, _key: SignIdentity | null, _chain: DelegationChain | null, _storage: AuthClientStorage, idleManager: IdleManager | undefined, _idpWindow?: Window | undefined, _eventHandler?: ((event: MessageEvent) => void) | undefined); | ||
private _handleSuccess; | ||
getIdentity(): Identity; | ||
isAuthenticated(): Promise<boolean>; | ||
login(options?: AuthClientLoginOptions): Promise<void>; | ||
/** | ||
* AuthClient Login - | ||
* Opens up a new window to authenticate with Internet Identity | ||
* @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 | ||
* windowOpenerFeatures: "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100", | ||
* onSuccess: () => { | ||
* console.log('Login Successful!'); | ||
* }, | ||
* onError: (error) => { | ||
* console.error('Login Failed: ', error); | ||
* } | ||
* }); | ||
*/ | ||
login(options?: { | ||
/** | ||
* Identity provider | ||
* @default "https://identity.ic0.app" | ||
*/ | ||
identityProvider?: string | URL; | ||
/** | ||
* Expiration of the authentication in nanoseconds | ||
* @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 | ||
*/ | ||
onSuccess?: (() => void) | (() => Promise<void>); | ||
/** | ||
* Callback in case authentication fails | ||
*/ | ||
onError?: ((error?: string) => void) | ((error?: string) => Promise<void>); | ||
}): Promise<void>; | ||
private _getEventHandler; | ||
@@ -70,0 +177,0 @@ private _handleFailure; |
@@ -0,4 +1,6 @@ | ||
/** @module AuthClient */ | ||
import { AnonymousIdentity, } from '@dfinity/agent'; | ||
import { isDelegationValid } from '@dfinity/authentication'; | ||
import { Delegation, DelegationChain, DelegationIdentity, Ed25519KeyIdentity, } from '@dfinity/identity'; | ||
import IdleManager from './idleManager'; | ||
const KEY_LOCALSTORAGE_KEY = 'identity'; | ||
@@ -47,4 +49,8 @@ const KEY_LOCALSTORAGE_DELEGATION = 'delegation'; | ||
} | ||
/** | ||
* Tool to manage authentication and identity | ||
* @see {@link AuthClient} | ||
*/ | ||
export class AuthClient { | ||
constructor(_identity, _key, _chain, _storage, | ||
constructor(_identity, _key, _chain, _storage, idleManager, | ||
// A handle on the IdP window. | ||
@@ -54,2 +60,3 @@ _idpWindow, | ||
_eventHandler) { | ||
var _a; | ||
this._identity = _identity; | ||
@@ -59,7 +66,28 @@ this._key = _key; | ||
this._storage = _storage; | ||
this.idleManager = idleManager; | ||
this._idpWindow = _idpWindow; | ||
this._eventHandler = _eventHandler; | ||
const logout = this.logout.bind(this); | ||
(_a = this.idleManager) === null || _a === void 0 ? void 0 : _a.registerCallback(logout); | ||
} | ||
/** | ||
* Create an AuthClient to manage authentication and identity | ||
* @constructs {@link AuthClient} | ||
* @param {AuthClientCreateOptions} options | ||
* @see {@link AuthClientCreateOptions} | ||
* @param options.identity Optional Identity to use as the base | ||
* @see {@link SignIdentity} | ||
* @param options.storage Storage mechanism for delegration credentials | ||
* @see {@link AuthClientStorage} | ||
* @param {IdleOptions} options.idleOptions Configures an {@link IdleManager} | ||
* @see {@link IdleOptions} | ||
* @example | ||
* const authClient = await AuthClient.create({ | ||
* idleOptions: { | ||
* disableIdle: true | ||
* } | ||
* }) | ||
*/ | ||
static async create(options = {}) { | ||
var _a; | ||
var _a, _b; | ||
const storage = (_a = options.storage) !== null && _a !== void 0 ? _a : new LocalStorage('ic-'); | ||
@@ -109,3 +137,6 @@ let key = null; | ||
} | ||
return new this(identity, key, chain, storage); | ||
const idleManager = ((_b = options.idleOptions) === null || _b === void 0 ? void 0 : _b.disableIdle) | ||
? undefined | ||
: IdleManager.create(options.idleOptions); | ||
return new this(identity, key, chain, storage, idleManager); | ||
} | ||
@@ -138,4 +169,27 @@ _handleSuccess(message, onSuccess) { | ||
} | ||
/** | ||
* AuthClient Login - | ||
* Opens up a new window to authenticate with Internet Identity | ||
* @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 | ||
* windowOpenerFeatures: "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100", | ||
* onSuccess: () => { | ||
* console.log('Login Successful!'); | ||
* }, | ||
* onError: (error) => { | ||
* console.error('Login Failed: ', error); | ||
* } | ||
* }); | ||
*/ | ||
async login(options) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d; | ||
let key = this._key; | ||
@@ -148,5 +202,4 @@ if (!key) { | ||
} | ||
// Set default maxTimeToLive to 1 day | ||
const defaultTimeToLive = | ||
/* days */ BigInt(1) * /* hours */ BigInt(24) * /* nanoseconds */ BigInt(3600000000000); | ||
// Set default maxTimeToLive to 8 hours | ||
const defaultTimeToLive = /* hours */ BigInt(8) * /* nanoseconds */ BigInt(3600000000000); | ||
// Create the URL of the IDP. (e.g. https://XXXX/#authorize) | ||
@@ -161,6 +214,7 @@ const identityProviderUrl = new URL(((_a = options === null || options === void 0 ? void 0 : options.identityProvider) === null || _a === void 0 ? void 0 : _a.toString()) || IDENTITY_PROVIDER_DEFAULT); | ||
// Add an event listener to handle responses. | ||
this._eventHandler = this._getEventHandler(identityProviderUrl, Object.assign({ maxTimeToLive: defaultTimeToLive }, options)); | ||
this._eventHandler = this._getEventHandler(identityProviderUrl, Object.assign({ maxTimeToLive: (_c = options === null || options === void 0 ? void 0 : options.maxTimeToLive) !== null && _c !== void 0 ? _c : defaultTimeToLive }, options)); | ||
window.addEventListener('message', this._eventHandler); | ||
// Open a new window with the IDP provider. | ||
this._idpWindow = (_c = window.open(identityProviderUrl.toString(), 'idpWindow')) !== null && _c !== void 0 ? _c : undefined; | ||
this._idpWindow = | ||
(_d = window.open(identityProviderUrl.toString(), 'idpWindow', options === null || options === void 0 ? void 0 : options.windowOpenerFeatures)) !== null && _d !== void 0 ? _d : undefined; | ||
// Check if the _idpWindow is closed by user. | ||
@@ -184,2 +238,3 @@ const checkInterruption = () => { | ||
if (event.origin !== identityProviderUrl.origin) { | ||
console.warn(`WARNING: expected origin '${identityProviderUrl.origin}', got '${event.origin}' (ignoring)`); | ||
return; | ||
@@ -186,0 +241,0 @@ } |
{ | ||
"name": "@dfinity/auth-client", | ||
"version": "0.10.4", | ||
"version": "0.11.0", | ||
"author": "DFINITY Stiftung <sdk@dfinity.org>", | ||
@@ -40,6 +40,6 @@ "license": "Apache-2.0", | ||
"peerDependencies": { | ||
"@dfinity/agent": "^0.10.4", | ||
"@dfinity/authentication": "^0.10.4", | ||
"@dfinity/identity": "^0.10.4", | ||
"@dfinity/principal": "^0.10.4" | ||
"@dfinity/agent": "^0.11.0", | ||
"@dfinity/authentication": "^0.11.0", | ||
"@dfinity/identity": "^0.11.0", | ||
"@dfinity/principal": "^0.11.0" | ||
}, | ||
@@ -54,3 +54,2 @@ "devDependencies": { | ||
"jest": "^27.3.1", | ||
"jest-expect-message": "^1.0.2", | ||
"node-fetch": "^2.6.7", | ||
@@ -57,0 +56,0 @@ "text-encoding": "^0.7.0", |
# @dfinity/auth-client | ||
> 0.10.5 Idle update - see changes [here](#0.10.5-idle-update) | ||
Simple interface to get your web application authenticated with the Internet Identity Service | ||
@@ -35,4 +37,6 @@ | ||
authClient.login({ | ||
// 7 days in nanoseconds | ||
maxTimeToLive: BigInt(7 * 24 * 60 * 60 * 1000 * 1000 * 1000), | ||
onSuccess: async () => { | ||
// authClient now has an identity | ||
handleAuthenticated(authClient); | ||
}, | ||
@@ -56,3 +60,46 @@ }); | ||
<h2 id="0.10.5-idle-update">Idle Update</h2> | ||
As of 0.10.5, the authClient has two notable new features: | ||
1. the maxTimeToLive is now a set to 8 hours by default, down from 24. | ||
2. you can now set a timeout for when your identity will be considered idle | ||
These defaults are more conservative, out of the interest of protecting users as more sites are starting to manage ICP and NFT's. You can override these defaults, and opt out of the Idle Manager if you so choose. For more details, see the [forum discussion](https://forum.dfinity.org/t/authclient-update-idle-timeouts). | ||
Additionally, we now support utility methods in Agents to invalidate an identity. It is suggested that you use this method to invalidate an identity once the user goes idle by calling `Actor.getAgent(actor).invalidateIdentity()`. See the below code for an example: | ||
```js | ||
const authClient = await AuthClient.create({ | ||
idleOptions: { | ||
idleTimeout: 1000 * 60 * 30, // set to 30 minutes | ||
} | ||
}); | ||
// ...authClient.login() | ||
const identity = await authClient.getIdentity(); | ||
const actor = Actor.createActor(idlFactory, { | ||
agent: new HttpAgent({ | ||
identity, | ||
}), | ||
canisterId, | ||
}); | ||
refreshLogin() { | ||
// prompt the user then refresh their authentication | ||
authClient.login({ | ||
onSuccess: async () => { | ||
const newIdentity = await AuthClient.getIdentity(); | ||
Actor.getAgent(actor).replaceIdentity(newIdentity); | ||
} | ||
}); | ||
} | ||
authClient.idleManager?.registerCallback?.(refreshLogin); | ||
``` | ||
In this code, we create an `authClient` with an idle timeout of 30 minutes. When the user is idle, we invalidate their identity and prompt them to login again. | ||
After the user logs in, we can set the new identity in the actor without reloading the page. | ||
Note: depends on [@dfinity/agent](https://www.npmjs.com/package/@dfinity/agent), [@dfinity/authentication](https://www.npmjs.com/package/@dfinity/authentication), and | ||
[@dfinity/identity](https://www.npmjs.com/package/@dfinity/identity). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
138667
14
19
1341
104
0