fix: Move request helper function (#2594)
* Move request method to passport utils * Use request method in OIDC provider
This commit is contained in:
parent
78464f315c
commit
0ed7286fc6
|
@ -1,14 +1,13 @@
|
|||
// @flow
|
||||
import passport from "@outlinewiki/koa-passport";
|
||||
import { Strategy as AzureStrategy } from "@outlinewiki/passport-azure-ad-oauth2";
|
||||
import fetch from "fetch-with-proxy";
|
||||
import jwt from "jsonwebtoken";
|
||||
import Router from "koa-router";
|
||||
import accountProvisioner from "../../../commands/accountProvisioner";
|
||||
import env from "../../../env";
|
||||
import { MicrosoftGraphError } from "../../../errors";
|
||||
import passportMiddleware from "../../../middlewares/passport";
|
||||
import { StateStore } from "../../../utils/passport";
|
||||
import { StateStore, request } from "../../../utils/passport";
|
||||
|
||||
const router = new Router();
|
||||
const providerName = "azure";
|
||||
|
@ -18,17 +17,6 @@ const AZURE_RESOURCE_APP_ID = process.env.AZURE_RESOURCE_APP_ID;
|
|||
|
||||
const scopes = [];
|
||||
|
||||
export async function request(endpoint: string, accessToken: string) {
|
||||
const response = await fetch(endpoint, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
name: "Microsoft",
|
||||
enabled: !!AZURE_CLIENT_ID,
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// @flow
|
||||
import passport from "@outlinewiki/koa-passport";
|
||||
import fetch from "fetch-with-proxy";
|
||||
import Router from "koa-router";
|
||||
import get from "lodash/get";
|
||||
import { Strategy } from "passport-oauth2";
|
||||
|
@ -12,7 +11,7 @@ import {
|
|||
} from "../../../errors";
|
||||
import passportMiddleware from "../../../middlewares/passport";
|
||||
import { getAllowedDomains } from "../../../utils/authentication";
|
||||
import { StateStore } from "../../../utils/passport";
|
||||
import { StateStore, request } from "../../../utils/passport";
|
||||
|
||||
const router = new Router();
|
||||
const providerName = "oidc";
|
||||
|
@ -36,18 +35,8 @@ const scopes = OIDC_SCOPES.split(" ");
|
|||
|
||||
Strategy.prototype.userProfile = async function (accessToken, done) {
|
||||
try {
|
||||
const response = await fetch(OIDC_USERINFO_URI, {
|
||||
credentials: "same-origin",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
return done(null, await response.json());
|
||||
} catch (err) {
|
||||
return done(err);
|
||||
}
|
||||
const response = await request(OIDC_USERINFO_URI, accessToken);
|
||||
return done(null, response);
|
||||
} catch (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
// @flow
|
||||
import { addMinutes, subMinutes } from "date-fns";
|
||||
import fetch from "fetch-with-proxy";
|
||||
import { type Request } from "koa";
|
||||
import { OAuthStateMismatchError } from "../errors";
|
||||
import { getCookieDomain } from "./domains";
|
||||
|
@ -47,3 +48,15 @@ export class StateStore {
|
|||
callback(null, true);
|
||||
};
|
||||
}
|
||||
|
||||
export async function request(endpoint: string, accessToken: string) {
|
||||
const response = await fetch(endpoint, {
|
||||
method: "GET",
|
||||
credentials: "same-origin",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
|
Reference in New Issue