fix(snjs): refreshing sessions (#2106)
* fix(snjs): refreshing sessions * fix(snjs): bring back all tests * fix(snjs): passing session tokens values * fix(api): remove redundant specs * fix(snjs): add projecting sessions to storage values * fix(snjs): deps tree * fix(snjs): bring back subscription tests * fix(snjs): remove only tag for migration tests * fix(snjs): session specs
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
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,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1 @@
|
||||
export * from './Generator'
|
||||
export * from './JwtSession'
|
||||
export * from './Session'
|
||||
export * from './TokenSession'
|
||||
export * from './Types'
|
||||
|
||||
Reference in New Issue
Block a user