|
|
@ -10,7 +10,7 @@ import { |
|
|
|
TokenGrantRequest, |
|
|
|
TokenGrantRequest, |
|
|
|
TokenGrantResponse, |
|
|
|
TokenGrantResponse, |
|
|
|
} from "@common/httpApi"; |
|
|
|
} from "@common/httpApi"; |
|
|
|
import { AccessToken, DeviceRegistrationToken, RefreshToken, TokenClaims } from "@common/TokenClaims"; |
|
|
|
import { AccessToken, DeviceRegistrationToken, DeviceToken, RefreshToken, TokenClaims } from "@common/TokenClaims"; |
|
|
|
import { User } from "../entities"; |
|
|
|
import { User } from "../entities"; |
|
|
|
import { ServerState } from "../state"; |
|
|
|
import { ServerState } from "../state"; |
|
|
|
|
|
|
|
|
|
|
@ -69,7 +69,8 @@ export function verifyToken<TClaims extends TokenClaims = TokenClaims>( |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
const claims: TokenClaims = decoded as any; |
|
|
|
const claims: TokenClaims = decoded as any; |
|
|
|
if (type != null && claims.type !== type) { |
|
|
|
if (type != null && claims.type !== type) { |
|
|
|
reject(new ApiError(`Expected a "${type} token, received a "${claims.type}" token`)); |
|
|
|
reject(new ApiError(`Expected a "${type}" token, received a "${claims.type}" token`, |
|
|
|
|
|
|
|
ErrorCode.BadToken)); |
|
|
|
} |
|
|
|
} |
|
|
|
resolve(claims as TClaims); |
|
|
|
resolve(claims as TClaims); |
|
|
|
} |
|
|
|
} |
|
|
@ -109,6 +110,15 @@ function generateDeviceRegistrationToken(secret: string): Promise<string> { |
|
|
|
return signToken(device_reg_token_claims); |
|
|
|
return signToken(device_reg_token_claims); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function generateDeviceToken(deviceId: string): Promise<string> { |
|
|
|
|
|
|
|
const device_token_claims: DeviceToken = { |
|
|
|
|
|
|
|
iss: ISSUER, |
|
|
|
|
|
|
|
type: "device", |
|
|
|
|
|
|
|
aud: deviceId, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
return signToken(device_token_claims); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function authentication(state: ServerState) { |
|
|
|
export function authentication(state: ServerState) { |
|
|
|
|
|
|
|
|
|
|
|
const router = Router(); |
|
|
|
const router = Router(); |
|
|
@ -201,12 +211,7 @@ export function verifyAuthorization(options?: Partial<VerifyAuthorizationOpts>): |
|
|
|
} |
|
|
|
} |
|
|
|
const token = matches[1]; |
|
|
|
const token = matches[1]; |
|
|
|
|
|
|
|
|
|
|
|
req.token = await verifyToken<AccessToken>(token, "access"); |
|
|
|
req.token = await verifyToken(token, opts.type) as any; |
|
|
|
|
|
|
|
|
|
|
|
if (req.token.type !== opts.type) { |
|
|
|
|
|
|
|
throw new ApiError(`Invalid token type "${req.token.type}", must be "${opts.type}"`, |
|
|
|
|
|
|
|
ErrorCode.BadToken); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
fun().then(() => next(null), (err) => next(err)); |
|
|
|
fun().then(() => next(null), (err) => next(err)); |
|
|
|
}; |
|
|
|
}; |
|
|
|