generated from klientjs/open-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcachips
authored and
Marcachips
committed
May 14, 2023
1 parent
e2d8eab
commit 24a46cf
Showing
22 changed files
with
760 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Changelog | ||
|
||
# 1.1.0 (2023-05-14) | ||
|
||
|
||
### Features | ||
|
||
* initialize klient jwt extension ([e2d8eab](https://github.com/klientjs/jwt/commit/e2d8eab20af7f2872ccc7cd8207c5eeedb0a024c)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Event, RequestEvent } from '@klient/core'; | ||
import type { AxiosError } from 'axios'; | ||
export default class CredentialsExpiredEvent extends Event { | ||
relatedEvent: RequestEvent; | ||
error: Error | AxiosError; | ||
static NAME: string; | ||
constructor(relatedEvent: RequestEvent, error: Error | AxiosError); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = require("@klient/core"); | ||
class CredentialsExpiredEvent extends core_1.Event { | ||
constructor(relatedEvent, error) { | ||
super(); | ||
this.relatedEvent = relatedEvent; | ||
this.error = error; | ||
} | ||
} | ||
exports.default = CredentialsExpiredEvent; | ||
CredentialsExpiredEvent.NAME = 'jwt:expired'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Event } from '@klient/core'; | ||
import type { AxiosResponse } from 'axios'; | ||
import type { AuthenticationState } from '../services/jwt'; | ||
export default class LoginEvent extends Event { | ||
response: AxiosResponse; | ||
state: AuthenticationState; | ||
decodedToken: unknown; | ||
static NAME: string; | ||
constructor(response: AxiosResponse, state: AuthenticationState, decodedToken: unknown); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = require("@klient/core"); | ||
class LoginEvent extends core_1.Event { | ||
constructor(response, state, decodedToken) { | ||
super(); | ||
this.response = response; | ||
this.state = state; | ||
this.decodedToken = decodedToken; | ||
} | ||
} | ||
exports.default = LoginEvent; | ||
LoginEvent.NAME = 'jwt:login'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Event } from '@klient/core'; | ||
export default class LogoutEvent extends Event { | ||
static NAME: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = require("@klient/core"); | ||
class LogoutEvent extends core_1.Event { | ||
} | ||
exports.default = LogoutEvent; | ||
LogoutEvent.NAME = 'jwt:logout'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Klient, { Request } from '@klient/core'; | ||
import type { JwtDecodeOptions } from 'jwt-decode'; | ||
import type { AxiosResponse, AxiosRequestConfig } from 'axios'; | ||
import type { KlientRequestConfig, Parameters as KlientParameters } from '@klient/core'; | ||
import type { StorageOptions } from '@klient/storage'; | ||
import JwtSecurity from './services/jwt'; | ||
import type { AuthenticationState } from './services/jwt'; | ||
export { default as LoginEvent } from './events/login'; | ||
export { default as ExpiredEvent } from './events/expired'; | ||
export { default as LogoutEvent } from './events/logout'; | ||
export { default as JwtSecurity } from './services/jwt'; | ||
export declare type Authenticate = (config: KlientRequestConfig, jwt: JwtSecurity) => void; | ||
export interface ConfigurableStep<T = unknown> extends AxiosRequestConfig { | ||
configure?: (credentials: T, config: KlientRequestConfig, jwt: JwtSecurity) => void; | ||
map?: (response: AxiosResponse, config: KlientRequestConfig, jwt: JwtSecurity) => AuthenticationState; | ||
} | ||
export interface Parameters extends KlientParameters { | ||
jwt?: { | ||
login?: ConfigurableStep; | ||
refresh?: ConfigurableStep<string>; | ||
authenticate?: Authenticate; | ||
storage?: { | ||
type: 'cookie' | 'static' | 'localStorage' | string; | ||
options?: StorageOptions; | ||
}; | ||
decode_options?: JwtDecodeOptions; | ||
}; | ||
} | ||
export declare const defaultParameters: Parameters; | ||
export interface KlientExtended extends Klient<Parameters> { | ||
jwt: JwtSecurity; | ||
login: <T>(data: unknown) => Request<T>; | ||
logout: () => Promise<void>; | ||
} | ||
export declare const extension: { | ||
name: string; | ||
initialize: (klient: Klient) => void; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.extension = exports.defaultParameters = exports.JwtSecurity = exports.LogoutEvent = exports.ExpiredEvent = exports.LoginEvent = void 0; | ||
const core_1 = require("@klient/core"); | ||
const jwt_1 = require("./services/jwt"); | ||
var login_1 = require("./events/login"); | ||
Object.defineProperty(exports, "LoginEvent", { enumerable: true, get: function () { return login_1.default; } }); | ||
var expired_1 = require("./events/expired"); | ||
Object.defineProperty(exports, "ExpiredEvent", { enumerable: true, get: function () { return expired_1.default; } }); | ||
var logout_1 = require("./events/logout"); | ||
Object.defineProperty(exports, "LogoutEvent", { enumerable: true, get: function () { return logout_1.default; } }); | ||
var jwt_2 = require("./services/jwt"); | ||
Object.defineProperty(exports, "JwtSecurity", { enumerable: true, get: function () { return jwt_2.default; } }); | ||
exports.defaultParameters = { | ||
jwt: { | ||
login: undefined, | ||
refresh: undefined, | ||
authenticate: undefined, | ||
storage: undefined, | ||
decode_options: undefined | ||
} | ||
}; | ||
exports.extension = { | ||
name: '@klient/jwt', | ||
initialize: (klient) => { | ||
klient.parameters.merge(exports.defaultParameters, { | ||
jwt: klient.parameters.get('jwt') || {} | ||
}); | ||
const jwt = new jwt_1.default(klient); | ||
klient.services.set('jwt', jwt); | ||
klient | ||
.extends('login', jwt.login.bind(jwt)) | ||
.extends('logout', jwt.logout.bind(jwt)) | ||
.extends('jwt', jwt); | ||
} | ||
}; | ||
core_1.Extensions.push(exports.extension); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { JwtDecodeOptions } from 'jwt-decode'; | ||
import Klient, { RequestEvent, KlientRequestConfig, Request } from '@klient/core'; | ||
import { Storage } from '@klient/storage'; | ||
import type { AxiosError, AxiosResponse } from 'axios'; | ||
import type { JwtPayload } from 'jwt-decode'; | ||
export declare type AnyObject = { | ||
[prop: string]: unknown; | ||
}; | ||
export interface AuthenticationState extends AnyObject { | ||
token: string; | ||
tokenExp?: number; | ||
refreshToken?: string; | ||
refreshTokenExp?: number; | ||
date?: number; | ||
} | ||
export interface DecodedToken { | ||
exp: number; | ||
} | ||
export declare const unixTimestamp: () => number; | ||
export default class JwtSecurity { | ||
protected readonly klient: Klient; | ||
static readonly ACTION_LOGIN = "jwt:login"; | ||
static readonly ACTION_REFRESH_CREDENTIALS = "jwt:refresh"; | ||
readonly storage: Storage | undefined; | ||
state: AuthenticationState | undefined; | ||
constructor(klient: Klient); | ||
login<T>(credentials: unknown): Promise<AxiosResponse<T, any>>; | ||
logout(): Promise<void>; | ||
refresh<T>(event?: RequestEvent): Promise<AxiosResponse<T, any>>; | ||
setupRequest(request: Request): void; | ||
decode(token: string, options?: JwtDecodeOptions): JwtPayload; | ||
setState(nextState?: AuthenticationState): void; | ||
protected refreshCredentials(event: RequestEvent): Promise<AxiosResponse<unknown, any>> | undefined; | ||
protected handleCredentialsExpired(event: RequestEvent, err: AxiosError | Error): Promise<never>; | ||
protected mapLoginResponseToState(response: AxiosResponse, request: KlientRequestConfig, isRefreshTokenResponse?: boolean): Promise<void>; | ||
protected getSecurityParameter(key: string, def?: unknown): unknown; | ||
get isAuthenticated(): boolean; | ||
get isTokenExpired(): boolean; | ||
get isRefreshTokenExpired(): boolean; | ||
get isCredentialsExpired(): boolean | null; | ||
get token(): string | undefined; | ||
get tokenExp(): number | undefined; | ||
get refreshToken(): string | undefined; | ||
get refreshTokenExp(): number | undefined; | ||
get authenticationDate(): Date | undefined; | ||
} |
Oops, something went wrong.