aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/users/login.ts
blob: b12b51b8c54149f7826c1a481f6f42b8fc8885f4 (plain) (blame)
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
import * as request from 'supertest'

import { ServerInfo } from '../server/servers'
import { getClient } from '../server/clients'

type Client = { id: string, secret: string }
type User = { username: string, password: string }
type Server = { url: string, client: Client, user: User }

function login (url: string, client: Client, user: User, expectedStatus = 200) {
  const path = '/api/v1/users/token'

  const body = {
    client_id: client.id,
    client_secret: client.secret,
    username: user.username,
    password: user.password,
    response_type: 'code',
    grant_type: 'password',
    scope: 'upload'
  }

  return request(url)
          .post(path)
          .type('form')
          .send(body)
          .expect(expectedStatus)
}

function logout (url: string, token: string, expectedStatus = 200) {
  const path = '/api/v1/users/revoke-token'

  return request(url)
    .post(path)
    .set('Authorization', 'Bearer ' + token)
    .type('form')
    .expect(expectedStatus)
}

async function serverLogin (server: Server) {
  const res = await login(server.url, server.client, server.user, 200)

  return res.body.access_token as string
}

function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = 200) {
  const path = '/api/v1/users/token'

  const body = {
    client_id: server.client.id,
    client_secret: server.client.secret,
    refresh_token: refreshToken,
    response_type: 'code',
    grant_type: 'refresh_token'
  }

  return request(server.url)
    .post(path)
    .type('form')
    .send(body)
    .expect(expectedStatus)
}

async function userLogin (server: Server, user: User, expectedStatus = 200) {
  const res = await login(server.url, server.client, user, expectedStatus)

  return res.body.access_token as string
}

async function getAccessToken (url: string, username: string, password: string) {
  const resClient = await getClient(url)
  const client = {
    id: resClient.body.client_id,
    secret: resClient.body.client_secret
  }

  const user = { username, password }

  try {
    const res = await login(url, client, user)
    return res.body.access_token
  } catch (err) {
    throw new Error('Cannot authenticate. Please check your username/password.')
  }
}

function setAccessTokensToServers (servers: ServerInfo[]) {
  const tasks: Promise<any>[] = []

  for (const server of servers) {
    const p = serverLogin(server).then(t => { server.accessToken = t })
    tasks.push(p)
  }

  return Promise.all(tasks)
}

// ---------------------------------------------------------------------------

export {
  login,
  logout,
  serverLogin,
  refreshToken,
  userLogin,
  getAccessToken,
  setAccessTokensToServers,
  Server,
  Client,
  User
}