interface OAuthConfig {
clientId: string;
redirectUri: string;
scope?: string[];
authority?: string;
popupRedirectUri?: string;
silentRedirectUri?: string;
automaticSilentRenew?: boolean;
env?: 'development' | 'production';
}clientId - OAuth client identifier from AuthSafe dashboardredirectUri - Callback URL after authenticationscope - Requested scopes (default: ['openid', 'email', 'profile'])authority - Custom authority URL (auto-detected if omitted)popupRedirectUri - Popup authentication callbacksilentRedirectUri - Silent token refresh callbackautomaticSilentRenew - Enable automatic token renewalenv - Environment for secure cookie settingsinterface User {
id_token: string;
session_state: string | null;
access_token: string;
refresh_token?: string;
token_type: string;
scope: string;
profile: UserProfile;
expires_at: number;
expires_in?: number;
expired?: boolean;
scopes: string[];
}interface UserProfile {
sub: string; // User ID
name?: string;
email?: string;
email_verified?: boolean;
picture?: string;
iss: string; // Issuer
aud: string; // Audience
exp: number; // Expiration time
iat: number; // Issued at
}interface TokenResponse {
access_token: string;
token_type: string;
expires_in: number;
refresh_token?: string;
id_token?: string;
scope?: string;
}interface SignoutOptions {
id_token_hint?: string;
post_logout_redirect_uri?: string;
state?: string;
}interface MfaMethod {
id: string;
type: 'totp' | 'email';
is_primary: boolean;
created_at: string;
last_used_at?: string;
}interface TotpSetupResponse {
secret: string; // BASE32 encoded secret
qr_code: string; // Data URL for QR code image
}interface MfaVerifyResponse {
success: boolean;
message?: string;
}interface SSOConnection {
id: string;
name: string;
provider: 'saml' | 'google' | 'microsoft' | 'github' | 'custom-oidc';
enabled: boolean;
created_at: string;
updated_at?: string;
metadata?: Record<string, any>;
}interface SAMLConfig {
entity_id: string;
sign_in_url: string;
certificate: string;
sign_request?: boolean;
signature_algorithm?: 'sha256' | 'sha512';
name_id_format?: string;
requested_attributes?: string[];
}interface OIDCConfig {
client_id: string;
client_secret: string;
authorization_endpoint: string;
token_endpoint: string;
userinfo_endpoint?: string;
jwks_uri?: string;
scope?: string[];
response_type?: string;
}interface UserProfile {
id: string;
email: string;
email_verified: boolean;
name?: string;
given_name?: string;
family_name?: string;
picture?: string;
locale?: string;
timezone?: string;
phone_number?: string;
phone_number_verified?: boolean;
created_at: string;
updated_at: string;
last_login?: string;
metadata?: Record<string, any>;
}interface UserUpdateData {
name?: string;
given_name?: string;
family_name?: string;
picture?: string;
locale?: string;
timezone?: string;
phone_number?: string;
metadata?: Record<string, any>;
}interface PasswordChangeData {
current_password: string;
new_password: string;
confirm_password?: string;
}interface BrandingConfig {
logo: string;
favicon: string;
primary_color: string;
secondary_color: string;
background_color: string;
text_color: string;
company_name: string;
support_email?: string;
privacy_url?: string;
terms_url?: string;
custom_css?: string;
}interface PasswordStrength {
score: number; // 0-4
strength: 'weak' | 'medium' | 'strong' | 'very-strong';
feedback: string[];
suggestions: string[];
}0 - Too weak1 - Weak2 - Medium3 - Strong4 - Very stronginterface ApiResponse<T> {
success: boolean;
data?: T;
error?: {
code: string;
message: string;
details?: any;
};
}interface PaginatedResponse<T> {
items: T[];
total: number;
page: number;
limit: number;
has_more: boolean;
}interface AuthError extends Error {
error: string;
error_description?: string;
state?: string;
}interface ApiError extends Error {
status: number;
code: string;
details?: any;
}import { User, OAuthConfig, MfaMethod } from 'authsafe-ts';
const config: OAuthConfig = {
clientId: 'my-client-id',
redirectUri: 'https://myapp.com/callback',
scope: ['openid', 'profile', 'email'],
};
const user: User | null = await oauthService.getUser();
if (user) {
const userId: string = user.profile.sub;
const email: string = user.profile.email!;
}function isTotpMethod(method: MfaMethod): boolean {
return method.type === 'totp';
}
function hasRefreshToken(
user: User | null,
): user is User & { refresh_token: string } {
return user !== null && typeof user.refresh_token === 'string';
}async function fetchData<T>(
httpService: HttpService,
endpoint: string,
): Promise<ApiResponse<T>> {
try {
const data = await httpService.get<T>(endpoint);
return { success: true, data };
} catch (error) {
return {
success: false,
error: {
code: 'API_ERROR',
message: error.message,
},
};
}
}