feat: add snjs package
This commit is contained in:
18
packages/snjs/lib/Services/Session/Sessions/Generator.ts
Normal file
18
packages/snjs/lib/Services/Session/Sessions/Generator.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { JwtSession } from './JwtSession'
|
||||
import { TokenSession } from './TokenSession'
|
||||
import { RawSessionPayload, RawStorageValue } from './Types'
|
||||
|
||||
export function SessionFromRawStorageValue(raw: RawStorageValue): JwtSession | TokenSession {
|
||||
if ('jwt' in raw) {
|
||||
return new JwtSession(raw.jwt as string)
|
||||
} else {
|
||||
const rawSession = raw as RawSessionPayload
|
||||
return new TokenSession(
|
||||
rawSession.accessToken,
|
||||
rawSession.accessExpiration,
|
||||
rawSession.refreshToken,
|
||||
rawSession.refreshExpiration,
|
||||
rawSession.readonlyAccess,
|
||||
)
|
||||
}
|
||||
}
|
||||
20
packages/snjs/lib/Services/Session/Sessions/JwtSession.ts
Normal file
20
packages/snjs/lib/Services/Session/Sessions/JwtSession.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Session } from './Session'
|
||||
|
||||
/** Legacy, for protocol versions <= 003 */
|
||||
|
||||
export class JwtSession extends Session {
|
||||
public jwt: string
|
||||
|
||||
constructor(jwt: string) {
|
||||
super()
|
||||
this.jwt = jwt
|
||||
}
|
||||
|
||||
public get authorizationValue(): string {
|
||||
return this.jwt
|
||||
}
|
||||
|
||||
public canExpire(): false {
|
||||
return false
|
||||
}
|
||||
}
|
||||
6
packages/snjs/lib/Services/Session/Sessions/Session.ts
Normal file
6
packages/snjs/lib/Services/Session/Sessions/Session.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export abstract class Session {
|
||||
public abstract canExpire(): boolean
|
||||
|
||||
/** Return the token that should be included in the header of authorized network requests */
|
||||
public abstract get authorizationValue(): string
|
||||
}
|
||||
46
packages/snjs/lib/Services/Session/Sessions/TokenSession.ts
Normal file
46
packages/snjs/lib/Services/Session/Sessions/TokenSession.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { SessionBody, SessionRenewalResponse } from '@standardnotes/responses'
|
||||
import { Session } from './Session'
|
||||
|
||||
/** For protocol versions >= 004 */
|
||||
export class TokenSession extends Session {
|
||||
static FromApiResponse(response: SessionRenewalResponse) {
|
||||
const body = response.data.session as SessionBody
|
||||
const accessToken: string = body.access_token
|
||||
const refreshToken: string = body.refresh_token
|
||||
const accessExpiration: number = body.access_expiration
|
||||
const refreshExpiration: number = body.refresh_expiration
|
||||
const readonlyAccess: boolean = body.readonly_access
|
||||
|
||||
return new TokenSession(accessToken, accessExpiration, refreshToken, refreshExpiration, readonlyAccess)
|
||||
}
|
||||
|
||||
constructor(
|
||||
public accessToken: string,
|
||||
public accessExpiration: number,
|
||||
public refreshToken: string,
|
||||
public refreshExpiration: number,
|
||||
private readonlyAccess: boolean,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
isReadOnly() {
|
||||
return this.readonlyAccess
|
||||
}
|
||||
|
||||
private getExpireAt() {
|
||||
return this.accessExpiration || 0
|
||||
}
|
||||
|
||||
public get authorizationValue() {
|
||||
return this.accessToken
|
||||
}
|
||||
|
||||
public canExpire() {
|
||||
return true
|
||||
}
|
||||
|
||||
public isExpired() {
|
||||
return this.getExpireAt() < Date.now()
|
||||
}
|
||||
}
|
||||
22
packages/snjs/lib/Services/Session/Sessions/Types.ts
Normal file
22
packages/snjs/lib/Services/Session/Sessions/Types.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Uuid } from '@standardnotes/common'
|
||||
|
||||
export type RawJwtPayload = {
|
||||
jwt?: string
|
||||
}
|
||||
|
||||
export type RawSessionPayload = {
|
||||
accessToken: string
|
||||
refreshToken: string
|
||||
accessExpiration: number
|
||||
refreshExpiration: number
|
||||
readonlyAccess: boolean
|
||||
}
|
||||
|
||||
export type RawStorageValue = RawJwtPayload | RawSessionPayload
|
||||
|
||||
export type RemoteSession = {
|
||||
uuid: Uuid
|
||||
updated_at: Date
|
||||
device_info: string
|
||||
current: boolean
|
||||
}
|
||||
5
packages/snjs/lib/Services/Session/Sessions/index.ts
Normal file
5
packages/snjs/lib/Services/Session/Sessions/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from './Generator'
|
||||
export * from './JwtSession'
|
||||
export * from './Session'
|
||||
export * from './TokenSession'
|
||||
export * from './Types'
|
||||
Reference in New Issue
Block a user