New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dfinity/auth-client

Package Overview
Dependencies
Maintainers
11
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dfinity/auth-client - npm Package Compare versions

Comparing version 0.8.6 to 0.8.7

42

lib/cjs/index.d.ts

@@ -6,8 +6,4 @@ import { Identity, SignIdentity } from '@dfinity/agent';

*/
export interface AuthClientOptions {
export interface AuthClientCreateOptions {
/**
* Identity provider. By default, use the identity service.
*/
identityProvider?: string | URL;
/**
* An identity to use as the base

@@ -21,2 +17,20 @@ */

}
export interface AuthClientLoginOptions {
/**
* Identity provider. By default, use the identity service.
*/
identityProvider?: string | URL;
/**
* Experiation of the authentication
*/
maxTimeToLive?: bigint;
/**
* Callback once login has completed
*/
onSuccess?: () => void;
/**
* Callback in case authentication fails
*/
onError?: (error?: string) => void;
}
/**

@@ -45,14 +59,12 @@ * Interface for persisting user authentication data

private _idpWindow?;
private _abortController?;
static create(options?: AuthClientOptions): Promise<AuthClient>;
protected constructor(_identity: Identity, _key: SignIdentity | null, _chain: DelegationChain | null, _storage: AuthClientStorage, _idpWindow?: Window | undefined, _abortController?: AbortController | undefined);
private _createDelegation;
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);
private _handleSuccess;
getIdentity(): Identity;
isAuthenticated(): Promise<boolean>;
login(options?: {
identityProvider?: string;
maxTimeToLive?: BigInt;
onSuccess?: (message?: string) => void;
onError?: (messate?: string) => void;
}): Promise<void>;
login(options?: AuthClientLoginOptions): Promise<void>;
private _getEventHandler;
private _handleFailure;
private _removeEventListener;
logout(options?: {

@@ -59,0 +71,0 @@ returnTo?: string;

@@ -53,4 +53,4 @@ "use strict";

_idpWindow,
// A controller used to remove the event listener in the login flow.
_abortController) {
// The event handler for processing events from the IdP.
_eventHandler) {
this._identity = _identity;

@@ -61,3 +61,3 @@ this._key = _key;

this._idpWindow = _idpWindow;
this._abortController = _abortController;
this._eventHandler = _eventHandler;
}

@@ -109,13 +109,11 @@ static async create(options = {}) {

}
async _createDelegation(message, event, onSuccess) {
var _a, _b;
// eslint-disable-next-line
// @ts-ignore (typescript doesn't understand adding an event listener with a signal).
_handleSuccess(message, onSuccess) {
var _a;
const delegations = message.delegations.map(signedDelegation => {
return {
delegation: new identity_1.Delegation(signedDelegation.delegation.pubkey, signedDelegation.delegation.expiration, signedDelegation.delegation.targets),
signature: signedDelegation.signature,
delegation: new identity_1.Delegation(agent_1.blobFromUint8Array(signedDelegation.delegation.pubkey), signedDelegation.delegation.expiration, signedDelegation.delegation.targets),
signature: agent_1.blobFromUint8Array(signedDelegation.signature),
};
});
const delegationChain = identity_1.DelegationChain.fromDelegations(delegations, agent_1.derBlobFromBlob(agent_1.blobFromUint8Array(event.data.userPublicKey)));
const delegationChain = identity_1.DelegationChain.fromDelegations(delegations, agent_1.derBlobFromBlob(agent_1.blobFromUint8Array(message.userPublicKey)));
const key = this._key;

@@ -126,7 +124,6 @@ if (!key) {

this._chain = delegationChain;
await this._storage.set(KEY_LOCALSTORAGE_DELEGATION, JSON.stringify(this._chain.toJSON()));
this._identity = identity_1.DelegationIdentity.fromDelegation(key, this._chain);
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.close();
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
(_b = this._abortController) === null || _b === void 0 ? void 0 : _b.abort(); // Send the abort signal to remove event listener.
this._removeEventListener();
}

@@ -140,3 +137,3 @@ getIdentity() {

async login(options) {
var _a, _b;
var _a, _b, _c;
let key = this._key;

@@ -150,3 +147,3 @@ if (!key) {

// Create the URL of the IDP. (e.g. https://XXXX/#authorize)
const identityProviderUrl = new URL((options === null || options === void 0 ? void 0 : options.identityProvider) || IDENTITY_PROVIDER_DEFAULT);
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);
// Set the correct hash if it isn't already set.

@@ -156,10 +153,13 @@ identityProviderUrl.hash = IDENTITY_PROVIDER_ENDPOINT;

// and event listeners.
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.close();
(_b = this._abortController) === null || _b === void 0 ? void 0 : _b.abort();
(_b = this._idpWindow) === null || _b === void 0 ? void 0 : _b.close();
this._removeEventListener();
// Add an event listener to handle responses.
// The event listener is associated with a controller that signals the listener
// to remove itself as soon as authentication is complete.
this._abortController = new AbortController();
window.addEventListener('message', async (event) => {
var _a, _b, _c, _d, _e;
this._eventHandler = this._getEventHandler(identityProviderUrl, 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;
}
_getEventHandler(identityProviderUrl, options) {
return async (event) => {
var _a, _b;
if (event.origin !== identityProviderUrl.origin) {

@@ -170,18 +170,29 @@ return;

switch (message.kind) {
case 'authorize-ready':
case 'authorize-ready': {
// IDP is ready. Send a message to request authorization.
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.postMessage({
const request = {
kind: 'authorize-client',
sessionPublicKey: (_b = this._key) === null || _b === void 0 ? void 0 : _b.getPublicKey().toDer(),
maxTimeToLive: options === null || options === void 0 ? void 0 : options.maxTimeToLive,
}, identityProviderUrl.origin);
sessionPublicKey: (_a = this._key) === null || _a === void 0 ? void 0 : _a.getPublicKey().toDer(),
maxTimetoLive: options === null || options === void 0 ? void 0 : options.maxTimeToLive,
};
(_b = this._idpWindow) === null || _b === void 0 ? void 0 : _b.postMessage(request, identityProviderUrl.origin);
break;
}
case 'authorize-client-success':
// Create the delegation chain and store it.
this._createDelegation(message, event, options === null || options === void 0 ? void 0 : options.onSuccess);
try {
this._handleSuccess(message, options === null || options === void 0 ? void 0 : options.onSuccess);
// Setting the storage is moved out of _handleSuccess to make
// it a sync function. Having _handleSuccess as an async function
// messes up the jest tests for some reason.
if (this._chain) {
await this._storage.set(KEY_LOCALSTORAGE_DELEGATION, JSON.stringify(this._chain.toJSON()));
}
}
catch (err) {
this._handleFailure(err.message, options === null || options === void 0 ? void 0 : options.onError);
}
break;
case 'authorize-client-failure':
(_c = this._idpWindow) === null || _c === void 0 ? void 0 : _c.close();
(_d = options === null || options === void 0 ? void 0 : options.onError) === null || _d === void 0 ? void 0 : _d.call(options, message.text);
(_e = this._abortController) === null || _e === void 0 ? void 0 : _e.abort(); // Send the abort signal to remove event listener.
this._handleFailure(message.text, options === null || options === void 0 ? void 0 : options.onError);
break;

@@ -191,8 +202,15 @@ default:

}
});
// Open a new window with the IDP provider.
const w = window.open(identityProviderUrl.toString(), 'idpWindow');
if (w) {
this._idpWindow = w;
};
}
_handleFailure(errorMessage, onError) {
var _a;
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.close();
onError === null || onError === void 0 ? void 0 : onError(errorMessage);
this._removeEventListener();
}
_removeEventListener() {
if (this._eventHandler) {
window.removeEventListener('message', this._eventHandler);
}
this._eventHandler = undefined;
}

@@ -199,0 +217,0 @@ async logout(options = {}) {

@@ -6,8 +6,4 @@ import { Identity, SignIdentity } from '@dfinity/agent';

*/
export interface AuthClientOptions {
export interface AuthClientCreateOptions {
/**
* Identity provider. By default, use the identity service.
*/
identityProvider?: string | URL;
/**
* An identity to use as the base

@@ -21,2 +17,20 @@ */

}
export interface AuthClientLoginOptions {
/**
* Identity provider. By default, use the identity service.
*/
identityProvider?: string | URL;
/**
* Experiation of the authentication
*/
maxTimeToLive?: bigint;
/**
* Callback once login has completed
*/
onSuccess?: () => void;
/**
* Callback in case authentication fails
*/
onError?: (error?: string) => void;
}
/**

@@ -45,14 +59,12 @@ * Interface for persisting user authentication data

private _idpWindow?;
private _abortController?;
static create(options?: AuthClientOptions): Promise<AuthClient>;
protected constructor(_identity: Identity, _key: SignIdentity | null, _chain: DelegationChain | null, _storage: AuthClientStorage, _idpWindow?: Window | undefined, _abortController?: AbortController | undefined);
private _createDelegation;
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);
private _handleSuccess;
getIdentity(): Identity;
isAuthenticated(): Promise<boolean>;
login(options?: {
identityProvider?: string;
maxTimeToLive?: BigInt;
onSuccess?: (message?: string) => void;
onError?: (messate?: string) => void;
}): Promise<void>;
login(options?: AuthClientLoginOptions): Promise<void>;
private _getEventHandler;
private _handleFailure;
private _removeEventListener;
logout(options?: {

@@ -59,0 +71,0 @@ returnTo?: string;

@@ -49,4 +49,4 @@ import { AnonymousIdentity, blobFromUint8Array, derBlobFromBlob, } from '@dfinity/agent';

_idpWindow,
// A controller used to remove the event listener in the login flow.
_abortController) {
// The event handler for processing events from the IdP.
_eventHandler) {
this._identity = _identity;

@@ -57,3 +57,3 @@ this._key = _key;

this._idpWindow = _idpWindow;
this._abortController = _abortController;
this._eventHandler = _eventHandler;
}

@@ -105,13 +105,11 @@ static async create(options = {}) {

}
async _createDelegation(message, event, onSuccess) {
var _a, _b;
// eslint-disable-next-line
// @ts-ignore (typescript doesn't understand adding an event listener with a signal).
_handleSuccess(message, onSuccess) {
var _a;
const delegations = message.delegations.map(signedDelegation => {
return {
delegation: new Delegation(signedDelegation.delegation.pubkey, signedDelegation.delegation.expiration, signedDelegation.delegation.targets),
signature: signedDelegation.signature,
delegation: new Delegation(blobFromUint8Array(signedDelegation.delegation.pubkey), signedDelegation.delegation.expiration, signedDelegation.delegation.targets),
signature: blobFromUint8Array(signedDelegation.signature),
};
});
const delegationChain = DelegationChain.fromDelegations(delegations, derBlobFromBlob(blobFromUint8Array(event.data.userPublicKey)));
const delegationChain = DelegationChain.fromDelegations(delegations, derBlobFromBlob(blobFromUint8Array(message.userPublicKey)));
const key = this._key;

@@ -122,7 +120,6 @@ if (!key) {

this._chain = delegationChain;
await this._storage.set(KEY_LOCALSTORAGE_DELEGATION, JSON.stringify(this._chain.toJSON()));
this._identity = DelegationIdentity.fromDelegation(key, this._chain);
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.close();
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
(_b = this._abortController) === null || _b === void 0 ? void 0 : _b.abort(); // Send the abort signal to remove event listener.
this._removeEventListener();
}

@@ -136,3 +133,3 @@ getIdentity() {

async login(options) {
var _a, _b;
var _a, _b, _c;
let key = this._key;

@@ -146,3 +143,3 @@ if (!key) {

// Create the URL of the IDP. (e.g. https://XXXX/#authorize)
const identityProviderUrl = new URL((options === null || options === void 0 ? void 0 : options.identityProvider) || IDENTITY_PROVIDER_DEFAULT);
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);
// Set the correct hash if it isn't already set.

@@ -152,10 +149,13 @@ identityProviderUrl.hash = IDENTITY_PROVIDER_ENDPOINT;

// and event listeners.
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.close();
(_b = this._abortController) === null || _b === void 0 ? void 0 : _b.abort();
(_b = this._idpWindow) === null || _b === void 0 ? void 0 : _b.close();
this._removeEventListener();
// Add an event listener to handle responses.
// The event listener is associated with a controller that signals the listener
// to remove itself as soon as authentication is complete.
this._abortController = new AbortController();
window.addEventListener('message', async (event) => {
var _a, _b, _c, _d, _e;
this._eventHandler = this._getEventHandler(identityProviderUrl, 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;
}
_getEventHandler(identityProviderUrl, options) {
return async (event) => {
var _a, _b;
if (event.origin !== identityProviderUrl.origin) {

@@ -166,18 +166,29 @@ return;

switch (message.kind) {
case 'authorize-ready':
case 'authorize-ready': {
// IDP is ready. Send a message to request authorization.
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.postMessage({
const request = {
kind: 'authorize-client',
sessionPublicKey: (_b = this._key) === null || _b === void 0 ? void 0 : _b.getPublicKey().toDer(),
maxTimeToLive: options === null || options === void 0 ? void 0 : options.maxTimeToLive,
}, identityProviderUrl.origin);
sessionPublicKey: (_a = this._key) === null || _a === void 0 ? void 0 : _a.getPublicKey().toDer(),
maxTimetoLive: options === null || options === void 0 ? void 0 : options.maxTimeToLive,
};
(_b = this._idpWindow) === null || _b === void 0 ? void 0 : _b.postMessage(request, identityProviderUrl.origin);
break;
}
case 'authorize-client-success':
// Create the delegation chain and store it.
this._createDelegation(message, event, options === null || options === void 0 ? void 0 : options.onSuccess);
try {
this._handleSuccess(message, options === null || options === void 0 ? void 0 : options.onSuccess);
// Setting the storage is moved out of _handleSuccess to make
// it a sync function. Having _handleSuccess as an async function
// messes up the jest tests for some reason.
if (this._chain) {
await this._storage.set(KEY_LOCALSTORAGE_DELEGATION, JSON.stringify(this._chain.toJSON()));
}
}
catch (err) {
this._handleFailure(err.message, options === null || options === void 0 ? void 0 : options.onError);
}
break;
case 'authorize-client-failure':
(_c = this._idpWindow) === null || _c === void 0 ? void 0 : _c.close();
(_d = options === null || options === void 0 ? void 0 : options.onError) === null || _d === void 0 ? void 0 : _d.call(options, message.text);
(_e = this._abortController) === null || _e === void 0 ? void 0 : _e.abort(); // Send the abort signal to remove event listener.
this._handleFailure(message.text, options === null || options === void 0 ? void 0 : options.onError);
break;

@@ -187,8 +198,15 @@ default:

}
});
// Open a new window with the IDP provider.
const w = window.open(identityProviderUrl.toString(), 'idpWindow');
if (w) {
this._idpWindow = w;
};
}
_handleFailure(errorMessage, onError) {
var _a;
(_a = this._idpWindow) === null || _a === void 0 ? void 0 : _a.close();
onError === null || onError === void 0 ? void 0 : onError(errorMessage);
this._removeEventListener();
}
_removeEventListener() {
if (this._eventHandler) {
window.removeEventListener('message', this._eventHandler);
}
this._eventHandler = undefined;
}

@@ -195,0 +213,0 @@ async logout(options = {}) {

{
"name": "@dfinity/auth-client",
"version": "0.8.6",
"version": "0.8.7",
"author": "DFINITY Stiftung <sdk@dfinity.org>",

@@ -42,5 +42,5 @@ "license": "Apache-2.0",

"dependencies": {
"@dfinity/agent": "^0.8.6",
"@dfinity/authentication": "^0.8.6",
"@dfinity/identity": "^0.8.6"
"@dfinity/agent": "^0.8.7",
"@dfinity/authentication": "^0.8.7",
"@dfinity/identity": "^0.8.7"
},

@@ -47,0 +47,0 @@ "devDependencies": {

@@ -22,8 +22,2 @@ # @dfinity/auth-client

```
import * as auth from "@dfinity/auth-client";
```
or using individual exports:
```
import { AuthClient } from "@dfinity/auth-client";

@@ -41,19 +35,17 @@ ```

```js
authClient.loginWithRedirect();
authClient.login({
onSuccess: async () => {
// authClient now has an identity
},
});
```
It handles redirects, saves your delegation to localStorage, and then sets you up with an identity.
It opens an `identity.ic0.app` window, saves your delegation to localStorage, and then sets you up with an identity.
```js
if (location.hash.substring(1).startsWith('access_token')) {
const identity = await authClient.handleRedirectCallback();
}
```
Then, you can use that identity to make authenticated calls using the `@dfinity/agent` `Actor`.
```js
const identity = await authClient.getIdentity();
const actor = Actor.createActor(idlFactory, {
agent: new HttpAgent({
host: hostUrlEl.value,
identity,

@@ -60,0 +52,0 @@ }),

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc