@nstudio/nativescript-auth0
Version:
Auth0 for NativeScript
330 lines • 9.64 kB
JavaScript
import { Device, Utils } from '@nativescript/core';
import { isRealDevice } from '@nativescript/core/utils';
export class Credentials {
static fromNative(credentials) {
if (credentials instanceof NSCAuth0Credentials) {
const ret = new Credentials();
ret.credentials = credentials;
return ret;
}
return null;
}
constructor(info) {
if (info) {
this.credentials = NSCAuth0Credentials.alloc().init(info.accessToken ?? null, info.tokenType ?? null, info.idToken ?? null, info.refreshToken ?? null, info.expiresIn ?? null, info.scope ?? null, info.recoveryCode ?? null);
}
}
get native() {
return this.credentials;
}
get accessToken() {
return this.credentials.accessToken;
}
get expiresIn() {
return this.credentials.expiresIn;
}
get idToken() {
return this.credentials.idToken;
}
get recoveryCode() {
return this.credentials.recoveryCode;
}
get refreshToken() {
return this.credentials.refreshToken;
}
get scope() {
return this.credentials.scope;
}
get tokenType() {
return this.credentials.tokenType;
}
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 NSCAuth0CredentialsManager) {
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, (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 NSCAuth0WebAuth) {
const ret = new WebAuth();
ret.webAuth = auth;
return ret;
}
return null;
}
get native() {
return this.webAuth;
}
useHTTPS() {
this.webAuth.useHTTPS();
return this;
}
start(options) {
return new Promise((resolve, reject) => {
this.webAuth.start(options?.scope, options?.audience, (credentials, error) => {
if (error) {
reject(error);
}
else {
resolve(Credentials.fromNative(credentials));
}
});
});
}
clear() {
return new Promise((resolve, reject) => {
this.webAuth.clearSessionWithFederated(false, (error) => {
if (error) {
reject(error);
}
else {
resolve();
}
});
});
}
}
export class JWT {
static fromNative(jwt) {
if (jwt instanceof NSCAuth0DecodedJWT) {
const ret = new JWT();
ret.jwt = jwt;
return ret;
}
return null;
}
get native() {
return this.jwt;
}
get identifier() {
return this.jwt.identifier;
}
get subject() {
return this.jwt.subject;
}
get issuer() {
return this.jwt.issuer;
}
get audience() {
return Utils.dataDeserialize(this.jwt.audience);
}
get issuedAt() {
return this.jwt.issuedAt;
}
get expiresAt() {
return this.jwt.expiresAt;
}
get notBefore() {
return this.jwt.notBefore;
}
get body() {
return Utils.dataDeserialize(this.jwt.body);
}
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(NSCAuth0.decodeJWTError(token));
}
export class UserInfo {
static fromNative(userInfo) {
if (userInfo instanceof NSCAuth0UserInfo) {
const ret = new UserInfo();
ret.userInfo = userInfo;
return ret;
}
return null;
}
get native() {
return this.userInfo;
}
get address() {
return Utils.dataDeserialize(this.userInfo.address);
}
get birthdate() {
return this.userInfo.birthdate;
}
get customClaims() {
return Utils.dataDeserialize(this.userInfo.customClaims);
}
get email() {
return this.userInfo.email;
}
get emailVerified() {
return this.userInfo.emailVerified;
}
get familyName() {
return this.userInfo.familyName;
}
get gender() {
return this.userInfo.gender;
}
get givenName() {
return this.userInfo.givenName;
}
get locale() {
return this.userInfo.locale.languageCode;
}
get middleName() {
return this.userInfo.middleName;
}
get name() {
return this.userInfo.name;
}
get nickname() {
return this.userInfo.nickname;
}
get phoneNumber() {
return this.userInfo.phoneNumber;
}
get phoneNumberVerified() {
return this.userInfo.phoneNumberVerified;
}
get picture() {
const picture = this.userInfo.picture;
if (picture) {
return picture.absoluteString;
}
return null;
}
get preferredUsername() {
return this.userInfo.preferredUsername;
}
get profile() {
const profile = this.userInfo.profile;
if (profile) {
return profile.absoluteString;
}
return null;
}
get sub() {
return this.userInfo.sub;
}
get updatedAt() {
return this.userInfo.updatedAt;
}
get website() {
const website = this.userInfo.website;
if (website) {
return website.absoluteString;
}
return null;
}
get zoneinfo() {
return this.userInfo.zoneinfo.name;
}
}
export class Authentication {
static fromNative(auth) {
if (auth instanceof NSCAuth0Authentication) {
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 ?? {}), (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 ?? {}), (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 ?? {}), (error) => {
if (error) {
reject(error);
}
else {
resolve();
}
});
});
}
}
export class Auth0 {
constructor(options) {
this.clientId = options.clientId;
this.domain = options.domain;
const sessionConfig = parseFloat(Device.sdkVersion) >= 18.0 && !isRealDevice() ? NSURLSessionConfiguration.ephemeralSessionConfiguration : NSURLSessionConfiguration.defaultSessionConfiguration;
const session = NSURLSession.sessionWithConfiguration(sessionConfig);
this.webAuth = WebAuth.fromNative(NSCAuth0.webAuthWithClientIdDomainSession(this.clientId, this.domain, session));
const authentication = NSCAuth0.authenticationWithClientIdDomainSession(this.clientId, this.domain, session);
this.auth = Authentication.fromNative(authentication);
this.credentialsManager = CredentialsManager.fromNative(NSCAuth0.credentialsManagerWithAuthentication(authentication));
}
}
//# sourceMappingURL=index.ios.js.map