File size: 3,137 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

// ---------------------------------------------------------------------------------------------
// login.ts
//
//  Purpose:  Handler for login function
//
// ---------------------------------------------------------------------------------------------

import { type FastifyRequest, type FastifyReply } from 'fastify';
import { type AuthIntegration } from 'integrations/Authentication/AuthIntegration';
import { type CredentialService } from 'services/CredentialsService/CredentialService';

const oauth2Handler = function (integration: AuthIntegration, config: any) {
  return {
    handler: async function (request: FastifyRequest, reply: FastifyReply) {
      const user = request.user;
      if (!user) {
        return await reply.code(401).send({ error: 'Unauthorized' });
      }

      // @ts-ignore
      const ns = request.query.ns;

      const vault = integration.manager.app.services.get('credentials') as CredentialService;
      const authUrl = await vault.generateAuthUrl(user, ns);

      reply.redirect(authUrl);
    },
    schema: {
      querystring: {
        type: 'object',
        properties: {
          ns: { type: 'string' }
        },
        required: ['ns']
      },
      response: {
        '4xx': {
          type: 'object',
          properties: {
            error: { type: 'string' }
          }
        },
        '3xx': {
          type: 'string'
        }
      }
    }
  };
};

const oauth2CallbackHandler = function (integration: AuthIntegration, config: any) {
  return {
    schema: {
      params: {
        type: 'object',
        properties: {
          ns: { type: 'string' }
        },
        required: ['ns']
      },
      querystring: {
        type: 'object',
        properties: {
          code: { type: 'string' },
          scope: { type: 'string' }
        },
        required: ['code', 'scope']
      },
      response: {
        302: {
          description: 'Redirection response',
          type: 'null' // Since no body is sent on a redirect
        },
        401: {
          type: 'object',
          properties: {
            error: { type: 'string' }
          }
        },
        500: {
          type: 'object',
          properties: {
            error: { type: 'string' }
          }
        }
      }
    },
    handler: async function (request: FastifyRequest, reply: FastifyReply) {
      const user = request.user;
      if (user == null) {
        return await reply.code(401).send({ error: 'Unauthorized' });
      }

      // @ts-ignore
      const ns = request.params.ns;
      // @ts-ignore
      const code = request.query.code;
      // @ts-ignore
      const scopes = request.query.scope;
      const vault = integration.manager.app.services.get('credentials') as CredentialService;
      const success = await vault.generateAccessToken(user, ns, code, scopes);

      if (success) {
        reply.redirect('/');
      } else {
        reply.code(500).send({ error: 'Failed to get access token' });
      }
    }
  };
};

export { oauth2Handler, oauth2CallbackHandler };