1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | const _ = require("lodash");
|
20 | const index_1 = require("../index");
|
21 | const index_2 = require("../http/index");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | async function getMicrosoftMe(token) {
|
30 | let accessToken;
|
31 | if (_.isObjectLike(token)) {
|
32 | accessToken = token.access_token
|
33 | .trim();
|
34 | }
|
35 | else {
|
36 | accessToken = index_1.toStringSafe(token)
|
37 | .trim();
|
38 | }
|
39 | try {
|
40 | const RESPONSE = await index_2.GET('https://graph.microsoft.com/v1.0/me', {
|
41 | headers: {
|
42 | 'Authorization': `Bearer ${accessToken}`
|
43 | }
|
44 | });
|
45 | if (200 === RESPONSE.code) {
|
46 | return JSON.parse((await RESPONSE.readBody())
|
47 | .toString('utf8'));
|
48 | }
|
49 | }
|
50 | catch (_a) { }
|
51 | return false;
|
52 | }
|
53 | exports.getMicrosoftMe = getMicrosoftMe;
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function getMicrosoftOAuthLoginUrl() {
|
60 | return `https://login.microsoftonline.com/${encodeURIComponent(process.env.MICROSOFT_OAUTH_TENANT_ID
|
61 | .trim())}/oauth2/authorize?client_id=${encodeURIComponent(process.env.MICROSOFT_OAUTH_CLIENT_ID
|
62 | .trim())}&response_type=code&redirect_uri=${encodeURIComponent(process.env.MICROSOFT_OAUTH_REDIRECT_URL
|
63 | .trim())}&response_mode=query&resource=${encodeURIComponent('https://graph.microsoft.com')}`;
|
64 | }
|
65 | exports.getMicrosoftOAuthLoginUrl = getMicrosoftOAuthLoginUrl;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function registerForMicrosoftOAuth(hostOrRouter, opts) {
|
73 | let redirectPath = index_1.toStringSafe(opts.redirectPath)
|
74 | .trim();
|
75 | if ('' === redirectPath) {
|
76 | redirectPath = '/oauth/microsoft';
|
77 | }
|
78 | hostOrRouter.get(redirectPath, async function (req, res, next) {
|
79 | try {
|
80 | const CODE = index_1.toStringSafe(req.query['code'])
|
81 | .trim();
|
82 | if ('' !== CODE) {
|
83 | const URL = `https://login.microsoftonline.com/${encodeURIComponent(process.env.MICROSOFT_OAUTH_TENANT_ID
|
84 | .trim())}/oauth2/token`;
|
85 | const BODY = Buffer.from(`grant_type=authorization_code` +
|
86 | `&client_id=${encodeURIComponent(process.env.MICROSOFT_OAUTH_CLIENT_ID
|
87 | .trim())}` +
|
88 | `&code=${encodeURIComponent(CODE)}` +
|
89 | `&redirect_uri=${encodeURIComponent(process.env.MICROSOFT_OAUTH_REDIRECT_URL
|
90 | .trim())}` +
|
91 | `&client_secret=${encodeURIComponent(process.env.MICROSOFT_OAUTH_CLIENT_SECRET
|
92 | .trim())}&scope=${encodeURIComponent('https://graph.microsoft.com/user.read')}`, 'ascii');
|
93 | const RESPONSE = await index_2.POST(URL, {
|
94 | body: BODY,
|
95 | headers: {
|
96 | 'Content-Length': '' + BODY.length,
|
97 | 'Content-Type': 'application/x-www-form-urlencoded'
|
98 | }
|
99 | });
|
100 | if (200 === RESPONSE.code) {
|
101 | const TOKEN = JSON.parse((await RESPONSE.readBody())
|
102 | .toString('utf8'));
|
103 | if (TOKEN) {
|
104 | await Promise.resolve(opts.onAccessToken(TOKEN, req, res));
|
105 | let onSuccess = opts.onSuccess;
|
106 | if (!onSuccess) {
|
107 | onSuccess = (req2, res2) => {
|
108 | return res2.status(200)
|
109 | .header('Content-type', 'text/plain; charset=utf-8')
|
110 | .send(Buffer.from('Authorization succeeded. You can close the browser now.', 'utf8'));
|
111 | };
|
112 | }
|
113 | return await Promise.resolve(onSuccess(req, res));
|
114 | }
|
115 | }
|
116 | }
|
117 | const ERROR = index_1.toStringSafe(req.query['error'])
|
118 | .trim();
|
119 | if ('' !== ERROR) {
|
120 | const DESCRIPTION = index_1.toStringSafe(req.query['error_description']).trim();
|
121 | let onError = opts.onError;
|
122 | if (!onError) {
|
123 | onError = (err, desc, req2, res2) => {
|
124 | return res2.status(200)
|
125 | .header('Content-type', 'text/plain; charset=utf-8')
|
126 | .send(`Authorization error '${err}': '${desc}'`);
|
127 | };
|
128 | }
|
129 | return await Promise.resolve(onError(ERROR, DESCRIPTION, req, res));
|
130 | }
|
131 | return res.status(400)
|
132 | .send();
|
133 | }
|
134 | catch (e) {
|
135 | let onServerError = opts.onServerError;
|
136 | if (!onServerError) {
|
137 | onServerError = (err, req2, res2) => {
|
138 | return res2.status(500)
|
139 | .send();
|
140 | };
|
141 | }
|
142 | return await Promise.resolve(onServerError(e, req, res));
|
143 | }
|
144 | });
|
145 | }
|
146 | exports.registerForMicrosoftOAuth = registerForMicrosoftOAuth;
|
147 |
|
\ | No newline at end of file |