UNPKG

4.2 kBJavaScriptView Raw
1// import { Config } from './Config'
2// import { Output } from './Output/index'
3// import 'isomorphic-fetch'
4// import * as cuid from 'scuid'
5// import * as opn from 'opn'
6// import { AuthTrigger } from './types/common'
7// import { Client } from './Client/Client'
8// import { GraphQLClient } from 'graphql-request'
9// import chalk from 'chalk'
10// import { Environment } from './Environment'
11// import * as jwtDecode from 'jwt-decode'
12// const debug = require('debug')('auth')
13// export class Auth {
14// out: Output
15// config: Config
16// authTrigger: AuthTrigger = 'auth'
17// client: Client
18// env: Environment
19// constructor(out: Output, config: Config, env: Environment, client: Client) {
20// this.out = out
21// this.config = config
22// this.client = client
23// this.env = env
24// }
25// setAuthTrigger(authTrigger: AuthTrigger) {
26// this.authTrigger = authTrigger
27// }
28// async ensureAuth(force: boolean = false) {
29// const localToken = this.env.activeCluster.token
30// let token = localToken
31// let valid: any = true
32// if (token) {
33// // valid = await this.validateAuthToken(token)
34// valid = this.fastValidation(token)
35// }
36// if (!valid) {
37// this.out.warn(`Received invalid token. Trying to authenticate ...`)
38// }
39// if (!token || !valid || force) {
40// token = await this.requestAuthToken()
41// }
42// this.env.setToken(token)
43// if (
44// this.env.activeCluster.local &&
45// token !== localToken
46// ) {
47// this.env.saveGlobalRC()
48// }
49// // return if we already had a token
50// return !!this.env.activeCluster.token
51// }
52// async setToken(token: string) {
53// const valid = await this.validateAuthToken(token)
54// if (valid) {
55// this.env.setToken(token)
56// } else {
57// this.env.setToken(undefined)
58// this.env.saveGlobalRC()
59// this.out.error(
60// `You provided an invalid token. You can run ${this.out.color.bold(
61// 'graphcool auth',
62// )} to receive a valid auth token`,
63// )
64// this.out.exit(1)
65// }
66// }
67// async requestAuthToken(): Promise<string> {
68// const cliToken = cuid()
69// const url = `${this.config.authUIEndpoint}?cliToken=${
70// cliToken
71// }&authTrigger=${this.authTrigger}`
72// this.out.log(`Auth URL: ${chalk.underline(url)}`)
73// this.out.action.start(`Authenticating`)
74// await fetch(`${this.config.authEndpoint}/create`, {
75// method: 'post',
76// headers: {
77// 'Content-Type': 'application/json',
78// },
79// body: JSON.stringify({ cliToken }),
80// })
81// opn(url)
82// let authToken
83// while (!authToken) {
84// const endpointUrl = `${this.config.authEndpoint}/${cliToken}`
85// const result = await fetch(endpointUrl)
86// const json = await result.json()
87// authToken = json.authToken
88// if (authToken) {
89// this.out.action.stop()
90// return authToken as string
91// }
92// await new Promise(r => setTimeout(r, 500))
93// }
94// return authToken
95// }
96// fastValidation(token: string): boolean {
97// try {
98// const decoded = jwtDecode(token)
99// return (
100// typeof decoded.iat === 'number' && typeof decoded.clientId === 'string'
101// )
102// } catch (e) {
103// return false
104// }
105// }
106// async validateAuthToken(token: string): Promise<string | null> {
107// const client = new GraphQLClient(
108// this.env.activeCluster.getDeployEndpoint(),
109// {
110// headers: {
111// Authorization: `Bearer ${token}`,
112// },
113// },
114// )
115// const authQuery = `{
116// viewer {
117// user {
118// id
119// email
120// }
121// }
122// }`
123// try {
124// const result = await client.request<{
125// viewer: { user: { email: string } }
126// }>(authQuery)
127// if (!result.viewer.user || !result.viewer.user.email) {
128// return null
129// }
130// return result.viewer.user.email
131// } catch (e) {
132// //
133// console.log(e)
134// }
135// return null
136// }
137// }
138//# sourceMappingURL=Auth.js.map
\No newline at end of file