UNPKG

@nstudio/nativescript-auth0

Version:
350 lines 10.4 kB
import { Utils } from '@nativescript/core'; export class Credentials { static fromNative(credentials) { if (credentials instanceof com.auth0.android.result.Credentials) { const ret = new Credentials(); ret.credentials = credentials; return ret; } return null; } constructor(info) { if (info) { let date; if (info.expiresIn && info.expiresIn instanceof Date) { date = info.expiresIn.getTime(); } else { date = Date.now() + 86400 * 1000; } this.credentials = io.nstudio.plugins.auth0.Auth0.createCredentials(info.accessToken ?? null, info.tokenType ?? null, info.idToken ?? null, info.refreshToken ?? null, date, info.scope ?? null, info.recoveryCode ?? null); } } get native() { return this.credentials; } get accessToken() { return this.credentials.getAccessToken(); } get expiresIn() { return this.credentials.getExpiresAt().getTime() - Date.now(); } get expiresAt() { return new Date(this.credentials.getExpiresAt().getTime()); } get idToken() { return this.credentials.getIdToken(); } get recoveryCode() { return this.credentials.getRecoveryCode(); } get refreshToken() { return this.credentials.getRefreshToken(); } get scope() { return this.credentials.getScope(); } get tokenType() { return this.credentials.getType(); } toJSON() { const redacted = '<REDACTED>'; return { accessToken: redacted, expiresIn: this.expiresIn, idToken: redacted, recoveryCode: this.recoveryCode, refreshToken: this.refreshToken ? redacted : null, scope: this.scope, tokenType: this.tokenType ? redacted : null, }; } } export class CredentialsManager { static fromNative(manager) { if (manager instanceof io.nstudio.plugins.auth0.CredentialsManager) { const ret = new CredentialsManager(); ret.credentialsManager = manager; return ret; } return null; } get native() { return this.credentialsManager; } clear() { this.credentialsManager.clear(); } credentials(scope, minTtl, parameters, forceRefresh) { return new Promise((resolve, reject) => { this.credentialsManager.credentials(scope ?? null, minTtl ?? 0, Utils.dataSerialize(parameters ?? {}), forceRefresh ?? false, new kotlin.jvm.functions.Function2({ invoke: (credentials, error) => { if (error) { reject(error); } else { resolve(Credentials.fromNative(credentials)); } }, })); }); } store(credentials) { this.credentialsManager.store(credentials.native); } hasValidCredentials(minTtl) { return this.credentialsManager.hasValid(minTtl ?? 0); } } export class WebAuth { static fromNative(auth) { if (auth instanceof io.nstudio.plugins.auth0.WebAuth) { const ret = new WebAuth(); ret.webAuth = auth; return ret; } return null; } get native() { return this.webAuth; } useHTTPS() { return this; } start(options) { return new Promise((resolve, reject) => { this.webAuth.start(Utils.android.getCurrentActivity(), options?.scheme ?? null, options.scope ?? null, options?.audience ?? null, options?.redirectUrl ?? null, new kotlin.jvm.functions.Function2({ invoke: (credentials, error) => { if (error) { reject(error); } else { resolve(Credentials.fromNative(credentials)); } }, })); }); } clear(options) { return new Promise((resolve, reject) => { this.webAuth.clearSession(Utils.android.getCurrentActivity(), options?.scheme ?? null, options?.federated ?? false, options?.returnToUrl ?? null, new kotlin.jvm.functions.Function1({ invoke: (error) => { if (error) { reject(error); } else { resolve(); } }, })); }); } } export class JWT { static fromNative(jwt) { if (jwt instanceof io.nstudio.plugins.auth0.DecodedJWT) { const ret = new JWT(); ret.jwt = jwt; return ret; } return null; } get native() { return this.jwt; } get identifier() { return this.jwt.getIdentifier(); } get subject() { return this.jwt.getSubject(); } get issuer() { return this.jwt.getIssuer(); } get audience() { return Utils.dataDeserialize(this.jwt.getAudience()); } get issuedAt() { const issuedAt = this.jwt.getIssuedAt(); if (issuedAt) { return new Date(issuedAt.getTime()); } return null; } get expiresAt() { const expiresAt = this.jwt.getExpiresAt(); if (expiresAt) { return new Date(expiresAt.getTime()); } return null; } get notBefore() { const nb = this.jwt.getNotBefore(); if (nb) { return new Date(nb.getTime()); } return null; } get body() { return Utils.dataDeserialize(this.jwt.getBody()); } toJSON() { return { identifier: this.identifier, subject: this.subject, issuer: this.issuer, audience: this.audience, issuedAt: this.issuedAt, expiresAt: this.expiresAt, notBefore: this.notBefore, body: this.body, }; } } export function decodeJWT(token) { return JWT.fromNative(io.nstudio.plugins.auth0.Auth0.decodeJWT(token)); } export class UserInfo { static fromNative(userInfo) { if (userInfo instanceof com.auth0.android.result.UserProfile) { const ret = new UserInfo(); ret.userInfo = userInfo; return ret; } return null; } get native() { return this.userInfo; } get address() { // todo return []; } get birthdate() { return null; } get customClaims() { return {}; } get email() { return null; } get emailVerified() { const verified = this.userInfo.isEmailVerified(); return verified.booleanValue?.() || verified; } get familyName() { return this.userInfo.getFamilyName(); } get gender() { return null; } get givenName() { return this.userInfo.getGivenName(); } get locale() { return null; } get middleName() { return null; } get name() { return this.userInfo.getName(); } get nickname() { return this.userInfo.getNickname(); } get phoneNumber() { return null; } get phoneNumberVerified() { return false; // todo } get picture() { return this.userInfo.getPictureURL(); } get preferredUsername() { return null; } get profile() { return null; } get sub() { return null; } get updatedAt() { return null; } get website() { return null; } get zoneinfo() { return null; } } export class Authentication { static fromNative(auth) { if (auth instanceof io.nstudio.plugins.auth0.Authentication) { const ret = new Authentication(); ret.authentication = auth; return ret; } return null; } get native() { return this.authentication; } userInfo(options) { return new Promise((resolve, reject) => { this.authentication.userInfo(options.accessToken, Utils.dataSerialize(options.headers ?? {}), new kotlin.jvm.functions.Function2({ invoke: (userInfo, error) => { if (error) { reject(error); } else { resolve(UserInfo.fromNative(userInfo)); } }, })); }); } refreshToken(options) { return new Promise((resolve, reject) => { this.authentication.refreshToken(options.refreshToken, options.scope ?? null, options?.audience ?? null, Utils.dataSerialize(options.headers ?? {}), new kotlin.jvm.functions.Function2({ invoke: (credentials, error) => { if (error) { reject(error); } else { resolve(Credentials.fromNative(credentials)); } }, })); }); } revoke(options) { return new Promise((resolve, reject) => { this.authentication.revoke(options.refreshToken, Utils.dataSerialize(options.headers ?? {}), new kotlin.jvm.functions.Function1({ invoke: (error) => { if (error) { reject(error); } else { resolve(); } }, })); }); } } export class Auth0 { constructor(options) { this.clientId = options.clientId; this.domain = options.domain; this.webAuth = WebAuth.fromNative(io.nstudio.plugins.auth0.Auth0.webAuth(this.clientId, this.domain)); const authentication = io.nstudio.plugins.auth0.Auth0.authentication(this.clientId, this.domain); this.auth = Authentication.fromNative(authentication); this.credentialsManager = CredentialsManager.fromNative(io.nstudio.plugins.auth0.Auth0.credentialsManager(Utils.android.getApplicationContext(), authentication)); } } //# sourceMappingURL=index.android.js.map