Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | 1x 1x 1x 8x 1x 1x 1x 1x 1x 8x 8x 3x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { getRandomString, createHash } from './util'
import { jwtVerify, importJWK, KeyLike } from 'jose'
export interface EveSSOPCKEAuthConfig {
clientId: string
redirectUri: string
}
/**
* A URI object for initiating authentication
*/
export interface EveSSOUri {
/**
* the uri to eve auth
*/
uri: string
/**
* The generated state string
*/
state: string
/**
* The generated code verifer. Must be saved for later use.
*/
codeVerifier: string
}
export interface EveSSOToken {
access_token: string
expires_in: string
token_type: string
refresh_token: string
payload?: EveSSOPayload
}
/**
* Eve SSO Token
*/
export interface EveSSOPayload {
scp: string
jti: string
kid: string
sub: string
azp: string
tenant: string
tier: string
region: string
name: string
owner: string
exp: number
iat: number
iss: string
}
/**
* Returns an new instance of EveSSOAuth
*
* @remarks
*
* This method is the main export of the package. It is used to instatiate the EveSSOAuth class.
*
* @param config - A config object
* @returns A EveSSOAuth object
*/
export function createSSO (config: EveSSOPCKEAuthConfig, fetch = window.fetch): EveSSOAuth {
return new EveSSOAuth(config, fetch)
}
const BASE_URI = 'https://login.eveonline.com/'
const AUTHORIZE_PATH = '/v2/oauth/authorize'
const TOKEN_PATH = '/v2/oauth/token'
const REVOKE_PATH = '/v2/oauth/revoke'
const JWKS_URL = 'https://login.eveonline.com/oauth/jwks'
class EveSSOAuth {
protected config: EveSSOPCKEAuthConfig
protected publicKey!: KeyLike
private readonly fetch
constructor (config: EveSSOPCKEAuthConfig, fetch = window.fetch) {
this.fetch = fetch
this.config = config
}
async generateState (): Promise<string> {
return await getRandomString(8)
}
async generateCodeVerifier (): Promise<string> {
return await getRandomString(64)
}
async generateCodeChallenge (codeVerifier: string): Promise<string> {
return await createHash(codeVerifier)
}
async _getJWKKeyData (): Promise<any> {
try {
const response = await fetch(JWKS_URL)
return await response.json()
} catch (error) {
console.log('There was an error retreiving JWK data', error)
}
}
async getPublicKey (): Promise<KeyLike> {
Iif (this.publicKey === undefined) {
try {
const jwks = await this._getJWKKeyData()
if (jwks !== null) {
const key = jwks.keys.find((x: any) => x.alg === 'RS256')
this.publicKey = await importJWK(key) as KeyLike
return this.publicKey
} else {
throw new Error('There was a problem obtaining public key')
}
} catch (error) {
console.log('There was an error retreiving the public key:', error)
}
}
return this.publicKey
}
/**
* Returns the an EveSSOUri that contains all the details required.
*
* @param scope - an array of strings for the scopes to request access to.
* @returns an EveSSOUri object
*/
async getUri (scope: string[] = []): Promise<EveSSOUri> {
const state = await this.generateState()
const codeVerifier = await this.generateCodeVerifier()
const codeChallenge = await this.generateCodeChallenge(codeVerifier)
const url = new URL(AUTHORIZE_PATH, BASE_URI)
url.searchParams.append('response_type', 'code')
url.searchParams.append('redirect_uri', this.config.redirectUri)
url.searchParams.append('client_id', this.config.clientId)
url.searchParams.append('code_challenge', codeChallenge)
url.searchParams.append('code_challenge_method', 'S256')
url.searchParams.append('scope', scope.join(' '))
url.searchParams.append('state', state)
return {
uri: url.toString(),
state,
codeVerifier
}
}
async verifyToken (token: EveSSOToken): Promise<EveSSOToken> {
const publicKey = await this.getPublicKey()
const { payload } = await jwtVerify(token.access_token, publicKey, {
issuer: 'login.eveonline.com'
})
token.payload = payload as unknown as EveSSOPayload
return token
}
async _fetchToken (url: string, init: object): Promise<Response> {
return await this.fetch(url, init)
}
/**
* Proceses the response from the Oauth server
* @param code - code returned from the auth serve
* @param codeVerifier - the code verifier generated from {@link EveSSOAuth.getUri | getUri}
* @returns A Promise of the EveSSOToken
*/
async getAccessToken (code: string, codeVerifier: string): Promise<EveSSOToken> {
try {
const form = new URLSearchParams()
form.append('grant_type', 'authorization_code')
form.append('code', code)
form.append('client_id', this.config.clientId)
form.append('code_verifier', codeVerifier)
const url = new URL(TOKEN_PATH, BASE_URI).toString()
const response = await this._fetchToken(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Host: 'login.eveonline.com'
},
body: form
})
const token = await this.verifyToken(await response.json())
return token
} catch (error) {
console.log('There was an error retreiving the token:', error)
throw error
}
}
/**
* Refresh your OAuth token
* @param refreshToken - refresh token returned from {@link }
* @param scopes - array of scopes to refresh for (optional)
* @returns a Promise of a new EveSSOToken
*/
async refreshToken (refreshToken: string, scopes?: string[]): Promise<EveSSOToken> {
try {
const form = new URLSearchParams()
form.append('grant_type', 'refresh_token')
form.append('refresh_token', refreshToken)
form.append('client_id', this.config.clientId)
Iif (scopes !== undefined) form.append('scope', scopes.join(' '))
const url = new URL(TOKEN_PATH, BASE_URI).toString()
const response = await this._fetchToken(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Host: 'login.eveonline.com'
},
body: form
})
const token = await this.verifyToken(await response.json())
return token
} catch (error) {
console.log('There was an error retreiving the token:', error)
throw error
}
}
/**
* Revoke refresh token
* @param token - refresh token that you want to revoke
*/
async revokeRefreshToken (token: EveSSOToken): Promise<void> {
try {
const form = new URLSearchParams()
form.append('token_type_hint', 'refresh_token')
form.append('token', token.refresh_token)
form.append('client_id', this.config.clientId)
const url = new URL(REVOKE_PATH, BASE_URI).toString()
const headers = new Headers()
headers.set('Content-Type', 'application/x-www-form-urlencoded')
headers.set('Host', 'login.eveonline.com')
await this._fetchToken(url, {
method: 'POST',
headers,
body: form
})
} catch (error) {
console.log(error)
throw error
}
}
}
export type { EveSSOAuth }
|