@backstage/plugin-auth-backend-module-microsoft-provider 0.2.0 → 0.2.1-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @backstage/plugin-auth-backend-module-microsoft-provider
2
2
 
3
+ ## 0.2.1-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 217458a: Updated configuration schema to include the new `allowedDomains` option for the `emailLocalPartMatchingUserEntityName` sign-in resolver.
8
+ - daa02d6: Add `skipUserProfile` config flag to Microsoft authenticator
9
+ - Updated dependencies
10
+ - @backstage/plugin-auth-node@0.5.3-next.1
11
+ - @backstage/backend-plugin-api@1.0.1-next.1
12
+
13
+ ## 0.2.1-next.0
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies
18
+ - @backstage/plugin-auth-node@0.5.3-next.0
19
+ - @backstage/backend-plugin-api@1.0.1-next.0
20
+
3
21
  ## 0.2.0
4
22
 
5
23
  ### Minor Changes
package/config.d.ts CHANGED
@@ -29,10 +29,14 @@ export interface Config {
29
29
  domainHint?: string;
30
30
  callbackUrl?: string;
31
31
  additionalScopes?: string | string[];
32
+ skipUserProfile?: boolean;
32
33
  signIn?: {
33
34
  resolvers: Array<
34
35
  | { resolver: 'emailMatchingUserEntityAnnotation' }
35
- | { resolver: 'emailLocalPartMatchingUserEntityName' }
36
+ | {
37
+ resolver: 'emailLocalPartMatchingUserEntityName';
38
+ allowedDomains?: string[];
39
+ }
36
40
  | { resolver: 'emailMatchingUserEntityProfileEmail' }
37
41
  >;
38
42
  };
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ var pluginAuthNode = require('@backstage/plugin-auth-node');
4
+ var strategy = require('./strategy.cjs.js');
5
+
6
+ const microsoftAuthenticator = pluginAuthNode.createOAuthAuthenticator({
7
+ defaultProfileTransform: pluginAuthNode.PassportOAuthAuthenticatorHelper.defaultProfileTransform,
8
+ scopes: {
9
+ required: ["email", "openid", "offline_access", "user.read"],
10
+ transform({ requested, granted, required, additional }) {
11
+ const hasResourceScope = Array.from(requested).some((s) => s.includes("/"));
12
+ if (hasResourceScope) {
13
+ return [...requested, "offline_access"];
14
+ }
15
+ return [...requested, ...granted, ...required, ...additional];
16
+ }
17
+ },
18
+ initialize({ callbackUrl, config }) {
19
+ const clientId = config.getString("clientId");
20
+ const clientSecret = config.getString("clientSecret");
21
+ const tenantId = config.getString("tenantId");
22
+ const domainHint = config.getOptionalString("domainHint");
23
+ const skipUserProfile = config.getOptionalBoolean("skipUserProfile") ?? false;
24
+ const strategy$1 = new strategy.ExtendedMicrosoftStrategy(
25
+ {
26
+ clientID: clientId,
27
+ clientSecret,
28
+ callbackURL: callbackUrl,
29
+ tenant: tenantId
30
+ },
31
+ (accessToken, refreshToken, params, fullProfile, done) => {
32
+ done(void 0, { fullProfile, params, accessToken }, { refreshToken });
33
+ }
34
+ );
35
+ strategy$1.setSkipUserProfile(skipUserProfile);
36
+ const helper = pluginAuthNode.PassportOAuthAuthenticatorHelper.from(strategy$1);
37
+ return {
38
+ helper,
39
+ domainHint
40
+ };
41
+ },
42
+ async start(input, ctx) {
43
+ const options = {
44
+ accessType: "offline"
45
+ };
46
+ if (ctx.domainHint !== void 0) {
47
+ options.domain_hint = ctx.domainHint;
48
+ }
49
+ return ctx.helper.start(input, options);
50
+ },
51
+ async authenticate(input, ctx) {
52
+ return ctx.helper.authenticate(input);
53
+ },
54
+ async refresh(input, ctx) {
55
+ return ctx.helper.refresh(input);
56
+ }
57
+ });
58
+
59
+ exports.microsoftAuthenticator = microsoftAuthenticator;
60
+ //# sourceMappingURL=authenticator.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authenticator.cjs.js","sources":["../src/authenticator.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n createOAuthAuthenticator,\n PassportOAuthAuthenticatorHelper,\n PassportOAuthDoneCallback,\n PassportProfile,\n} from '@backstage/plugin-auth-node';\nimport { ExtendedMicrosoftStrategy } from './strategy';\n\n/** @public */\nexport const microsoftAuthenticator = createOAuthAuthenticator({\n defaultProfileTransform:\n PassportOAuthAuthenticatorHelper.defaultProfileTransform,\n scopes: {\n required: ['email', 'openid', 'offline_access', 'user.read'],\n transform({ requested, granted, required, additional }) {\n // Resources scopes are of the form `<resource>/<scope>`, and are handled\n // separately from the normal scopes in the client. When request a\n // resource scope we should only include forward the request scope along\n // with offline_access.\n const hasResourceScope = Array.from(requested).some(s => s.includes('/'));\n if (hasResourceScope) {\n return [...requested, 'offline_access'];\n }\n return [...requested, ...granted, ...required, ...additional];\n },\n },\n initialize({ callbackUrl, config }) {\n const clientId = config.getString('clientId');\n const clientSecret = config.getString('clientSecret');\n const tenantId = config.getString('tenantId');\n const domainHint = config.getOptionalString('domainHint');\n const skipUserProfile =\n config.getOptionalBoolean('skipUserProfile') ?? false;\n\n const strategy = new ExtendedMicrosoftStrategy(\n {\n clientID: clientId,\n clientSecret: clientSecret,\n callbackURL: callbackUrl,\n tenant: tenantId,\n },\n (\n accessToken: string,\n refreshToken: string,\n params: any,\n fullProfile: PassportProfile,\n done: PassportOAuthDoneCallback,\n ) => {\n done(undefined, { fullProfile, params, accessToken }, { refreshToken });\n },\n );\n\n strategy.setSkipUserProfile(skipUserProfile);\n const helper = PassportOAuthAuthenticatorHelper.from(strategy);\n\n return {\n helper,\n domainHint,\n };\n },\n\n async start(input, ctx) {\n const options: Record<string, string> = {\n accessType: 'offline',\n };\n\n if (ctx.domainHint !== undefined) {\n options.domain_hint = ctx.domainHint;\n }\n\n return ctx.helper.start(input, options);\n },\n\n async authenticate(input, ctx) {\n return ctx.helper.authenticate(input);\n },\n\n async refresh(input, ctx) {\n return ctx.helper.refresh(input);\n },\n});\n"],"names":["createOAuthAuthenticator","PassportOAuthAuthenticatorHelper","strategy","ExtendedMicrosoftStrategy"],"mappings":";;;;;AAyBO,MAAM,yBAAyBA,uCAAyB,CAAA;AAAA,EAC7D,yBACEC,+CAAiC,CAAA,uBAAA;AAAA,EACnC,MAAQ,EAAA;AAAA,IACN,QAAU,EAAA,CAAC,OAAS,EAAA,QAAA,EAAU,kBAAkB,WAAW,CAAA;AAAA,IAC3D,UAAU,EAAE,SAAA,EAAW,OAAS,EAAA,QAAA,EAAU,YAAc,EAAA;AAKtD,MAAM,MAAA,gBAAA,GAAmB,KAAM,CAAA,IAAA,CAAK,SAAS,CAAA,CAAE,KAAK,CAAK,CAAA,KAAA,CAAA,CAAE,QAAS,CAAA,GAAG,CAAC,CAAA,CAAA;AACxE,MAAA,IAAI,gBAAkB,EAAA;AACpB,QAAO,OAAA,CAAC,GAAG,SAAA,EAAW,gBAAgB,CAAA,CAAA;AAAA,OACxC;AACA,MAAO,OAAA,CAAC,GAAG,SAAW,EAAA,GAAG,SAAS,GAAG,QAAA,EAAU,GAAG,UAAU,CAAA,CAAA;AAAA,KAC9D;AAAA,GACF;AAAA,EACA,UAAW,CAAA,EAAE,WAAa,EAAA,MAAA,EAAU,EAAA;AAClC,IAAM,MAAA,QAAA,GAAW,MAAO,CAAA,SAAA,CAAU,UAAU,CAAA,CAAA;AAC5C,IAAM,MAAA,YAAA,GAAe,MAAO,CAAA,SAAA,CAAU,cAAc,CAAA,CAAA;AACpD,IAAM,MAAA,QAAA,GAAW,MAAO,CAAA,SAAA,CAAU,UAAU,CAAA,CAAA;AAC5C,IAAM,MAAA,UAAA,GAAa,MAAO,CAAA,iBAAA,CAAkB,YAAY,CAAA,CAAA;AACxD,IAAA,MAAM,eACJ,GAAA,MAAA,CAAO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K,CAAA,CAAA;AAAA,GACjC;AACF,CAAC;;;;"}
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ var module$1 = require('./module.cjs.js');
4
+
5
+ const authModuleMicrosoftProvider = module$1.authModuleMicrosoftProvider;
6
+
7
+ exports.authModuleMicrosoftProvider = authModuleMicrosoftProvider;
8
+ //# sourceMappingURL=deprecated.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deprecated.cjs.js","sources":["../src/deprecated.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { authModuleMicrosoftProvider as deprecatedAuthModuleMicrosoftProvider } from './module';\n\n/**\n * @public\n * @deprecated Use default import instead\n */\nexport const authModuleMicrosoftProvider =\n deprecatedAuthModuleMicrosoftProvider;\n"],"names":["deprecatedAuthModuleMicrosoftProvider"],"mappings":";;;;AAsBO,MAAM,2BACX,GAAAA;;;;"}
package/dist/index.cjs.js CHANGED
@@ -2,197 +2,18 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var pluginAuthNode = require('@backstage/plugin-auth-node');
6
- var jose = require('jose');
7
- var fetch = require('node-fetch');
8
- var passportMicrosoft = require('passport-microsoft');
9
- var backendPluginApi = require('@backstage/backend-plugin-api');
5
+ var authenticator = require('./authenticator.cjs.js');
6
+ var module$1 = require('./module.cjs.js');
7
+ var resolvers = require('./resolvers.cjs.js');
8
+ var deprecated = require('./deprecated.cjs.js');
10
9
 
11
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
12
10
 
13
- var fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
14
11
 
15
- class ExtendedMicrosoftStrategy extends passportMicrosoft.Strategy {
16
- userProfile(accessToken, done) {
17
- if (this.skipUserProfile(accessToken)) {
18
- done(null, void 0);
19
- return;
20
- }
21
- super.userProfile(
22
- accessToken,
23
- (err, profile) => {
24
- if (!profile || profile.photos) {
25
- done(err, profile);
26
- return;
27
- }
28
- this.getProfilePhotos(accessToken).then((photos) => {
29
- profile.photos = photos;
30
- done(err, profile);
31
- });
32
- }
33
- );
34
- }
35
- hasGraphReadScope(accessToken) {
36
- const { aud, scp } = jose.decodeJwt(accessToken);
37
- return aud === "00000003-0000-0000-c000-000000000000" && !!scp && scp.split(" ").map((s) => s.toLocaleLowerCase("en-US")).some(
38
- (s) => [
39
- "https://graph.microsoft.com/user.read",
40
- "https://graph.microsoft.com/user.read.all",
41
- "user.read",
42
- "user.read.all"
43
- ].includes(s)
44
- );
45
- }
46
- skipUserProfile(accessToken) {
47
- try {
48
- return !this.hasGraphReadScope(accessToken);
49
- } catch {
50
- return false;
51
- }
52
- }
53
- async getProfilePhotos(accessToken) {
54
- return this.getCurrentUserPhoto(accessToken, "96x96").then(
55
- (photo) => photo ? [{ value: photo }] : void 0
56
- );
57
- }
58
- async getCurrentUserPhoto(accessToken, size) {
59
- try {
60
- const res = await fetch__default.default(
61
- `https://graph.microsoft.com/v1.0/me/photos/${size}/$value`,
62
- {
63
- headers: {
64
- Authorization: `Bearer ${accessToken}`
65
- }
66
- }
67
- );
68
- const data = await res.buffer();
69
- return `data:image/jpeg;base64,${data.toString("base64")}`;
70
- } catch (error) {
71
- return void 0;
72
- }
73
- }
74
- }
75
-
76
- const microsoftAuthenticator = pluginAuthNode.createOAuthAuthenticator({
77
- defaultProfileTransform: pluginAuthNode.PassportOAuthAuthenticatorHelper.defaultProfileTransform,
78
- scopes: {
79
- required: ["email", "openid", "offline_access", "user.read"],
80
- transform({ requested, granted, required, additional }) {
81
- const hasResourceScope = Array.from(requested).some((s) => s.includes("/"));
82
- if (hasResourceScope) {
83
- return [...requested, "offline_access"];
84
- }
85
- return [...requested, ...granted, ...required, ...additional];
86
- }
87
- },
88
- initialize({ callbackUrl, config }) {
89
- const clientId = config.getString("clientId");
90
- const clientSecret = config.getString("clientSecret");
91
- const tenantId = config.getString("tenantId");
92
- const domainHint = config.getOptionalString("domainHint");
93
- const helper = pluginAuthNode.PassportOAuthAuthenticatorHelper.from(
94
- new ExtendedMicrosoftStrategy(
95
- {
96
- clientID: clientId,
97
- clientSecret,
98
- callbackURL: callbackUrl,
99
- tenant: tenantId
100
- },
101
- (accessToken, refreshToken, params, fullProfile, done) => {
102
- done(
103
- void 0,
104
- { fullProfile, params, accessToken },
105
- { refreshToken }
106
- );
107
- }
108
- )
109
- );
110
- return {
111
- helper,
112
- domainHint
113
- };
114
- },
115
- async start(input, ctx) {
116
- const options = {
117
- accessType: "offline"
118
- };
119
- if (ctx.domainHint !== void 0) {
120
- options.domain_hint = ctx.domainHint;
121
- }
122
- return ctx.helper.start(input, options);
123
- },
124
- async authenticate(input, ctx) {
125
- return ctx.helper.authenticate(input);
126
- },
127
- async refresh(input, ctx) {
128
- return ctx.helper.refresh(input);
129
- }
130
- });
131
-
132
- exports.microsoftSignInResolvers = void 0;
133
- ((microsoftSignInResolvers2) => {
134
- microsoftSignInResolvers2.emailMatchingUserEntityAnnotation = pluginAuthNode.createSignInResolverFactory({
135
- create() {
136
- return async (info, ctx) => {
137
- const { profile } = info;
138
- if (!profile.email) {
139
- throw new Error("Microsoft profile contained no email");
140
- }
141
- return ctx.signInWithCatalogUser({
142
- annotations: {
143
- "microsoft.com/email": profile.email
144
- }
145
- });
146
- };
147
- }
148
- });
149
- microsoftSignInResolvers2.userIdMatchingUserEntityAnnotation = pluginAuthNode.createSignInResolverFactory(
150
- {
151
- create() {
152
- return async (info, ctx) => {
153
- const { result } = info;
154
- const id = result.fullProfile.id;
155
- if (!id) {
156
- throw new Error("Microsoft profile contained no id");
157
- }
158
- return ctx.signInWithCatalogUser({
159
- annotations: {
160
- "graph.microsoft.com/user-id": id
161
- }
162
- });
163
- };
164
- }
165
- }
166
- );
167
- })(exports.microsoftSignInResolvers || (exports.microsoftSignInResolvers = {}));
168
-
169
- const authModuleMicrosoftProvider$1 = backendPluginApi.createBackendModule({
170
- pluginId: "auth",
171
- moduleId: "microsoft-provider",
172
- register(reg) {
173
- reg.registerInit({
174
- deps: {
175
- providers: pluginAuthNode.authProvidersExtensionPoint
176
- },
177
- async init({ providers }) {
178
- providers.registerProvider({
179
- providerId: "microsoft",
180
- factory: pluginAuthNode.createOAuthProviderFactory({
181
- authenticator: microsoftAuthenticator,
182
- signInResolverFactories: {
183
- ...exports.microsoftSignInResolvers,
184
- ...pluginAuthNode.commonSignInResolvers
185
- }
186
- })
187
- });
188
- }
189
- });
190
- }
12
+ exports.microsoftAuthenticator = authenticator.microsoftAuthenticator;
13
+ exports.default = module$1.authModuleMicrosoftProvider;
14
+ Object.defineProperty(exports, "microsoftSignInResolvers", {
15
+ enumerable: true,
16
+ get: function () { return resolvers.microsoftSignInResolvers; }
191
17
  });
192
-
193
- const authModuleMicrosoftProvider = authModuleMicrosoftProvider$1;
194
-
195
- exports.authModuleMicrosoftProvider = authModuleMicrosoftProvider;
196
- exports.default = authModuleMicrosoftProvider$1;
197
- exports.microsoftAuthenticator = microsoftAuthenticator;
18
+ exports.authModuleMicrosoftProvider = deprecated.authModuleMicrosoftProvider;
198
19
  //# sourceMappingURL=index.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/strategy.ts","../src/authenticator.ts","../src/resolvers.ts","../src/module.ts","../src/deprecated.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PassportProfile } from '@backstage/plugin-auth-node';\nimport { decodeJwt } from 'jose';\nimport fetch from 'node-fetch';\nimport { Strategy as MicrosoftStrategy } from 'passport-microsoft';\n\nexport class ExtendedMicrosoftStrategy extends MicrosoftStrategy {\n userProfile(\n accessToken: string,\n done: (err?: unknown, profile?: PassportProfile) => void,\n ): void {\n if (this.skipUserProfile(accessToken)) {\n done(null, undefined);\n return;\n }\n\n super.userProfile(\n accessToken,\n (err?: unknown, profile?: PassportProfile) => {\n if (!profile || profile.photos) {\n done(err, profile);\n return;\n }\n\n this.getProfilePhotos(accessToken).then(photos => {\n profile.photos = photos;\n done(err, profile);\n });\n },\n );\n }\n\n private hasGraphReadScope(accessToken: string): boolean {\n const { aud, scp } = decodeJwt(accessToken);\n return (\n aud === '00000003-0000-0000-c000-000000000000' &&\n !!scp &&\n (scp as string)\n .split(' ')\n .map(s => s.toLocaleLowerCase('en-US'))\n .some(s =>\n [\n 'https://graph.microsoft.com/user.read',\n 'https://graph.microsoft.com/user.read.all',\n 'user.read',\n 'user.read.all',\n ].includes(s),\n )\n );\n }\n\n private skipUserProfile(accessToken: string): boolean {\n try {\n return !this.hasGraphReadScope(accessToken);\n } catch {\n // If there is any error with checking the scope\n // we fall back to not skipping the user profile\n // which may still result in an auth failure\n // e.g. due to a foreign scope.\n return false;\n }\n }\n\n private async getProfilePhotos(\n accessToken: string,\n ): Promise<Array<{ value: string }> | undefined> {\n return this.getCurrentUserPhoto(accessToken, '96x96').then(photo =>\n photo ? [{ value: photo }] : undefined,\n );\n }\n\n private async getCurrentUserPhoto(\n accessToken: string,\n size: string,\n ): Promise<string | undefined> {\n try {\n const res = await fetch(\n `https://graph.microsoft.com/v1.0/me/photos/${size}/$value`,\n {\n headers: {\n Authorization: `Bearer ${accessToken}`,\n },\n },\n );\n const data = await res.buffer();\n\n return `data:image/jpeg;base64,${data.toString('base64')}`;\n } catch (error) {\n return undefined;\n }\n }\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n createOAuthAuthenticator,\n PassportOAuthAuthenticatorHelper,\n PassportOAuthDoneCallback,\n PassportProfile,\n} from '@backstage/plugin-auth-node';\nimport { ExtendedMicrosoftStrategy } from './strategy';\n\n/** @public */\nexport const microsoftAuthenticator = createOAuthAuthenticator({\n defaultProfileTransform:\n PassportOAuthAuthenticatorHelper.defaultProfileTransform,\n scopes: {\n required: ['email', 'openid', 'offline_access', 'user.read'],\n transform({ requested, granted, required, additional }) {\n // Resources scopes are of the form `<resource>/<scope>`, and are handled\n // separately from the normal scopes in the client. When request a\n // resource scope we should only include forward the request scope along\n // with offline_access.\n const hasResourceScope = Array.from(requested).some(s => s.includes('/'));\n if (hasResourceScope) {\n return [...requested, 'offline_access'];\n }\n return [...requested, ...granted, ...required, ...additional];\n },\n },\n initialize({ callbackUrl, config }) {\n const clientId = config.getString('clientId');\n const clientSecret = config.getString('clientSecret');\n const tenantId = config.getString('tenantId');\n const domainHint = config.getOptionalString('domainHint');\n\n const helper = PassportOAuthAuthenticatorHelper.from(\n new ExtendedMicrosoftStrategy(\n {\n clientID: clientId,\n clientSecret: clientSecret,\n callbackURL: callbackUrl,\n tenant: tenantId,\n },\n (\n accessToken: string,\n refreshToken: string,\n params: any,\n fullProfile: PassportProfile,\n done: PassportOAuthDoneCallback,\n ) => {\n done(\n undefined,\n { fullProfile, params, accessToken },\n { refreshToken },\n );\n },\n ),\n );\n\n return {\n helper,\n domainHint,\n };\n },\n\n async start(input, ctx) {\n const options: Record<string, string> = {\n accessType: 'offline',\n };\n\n if (ctx.domainHint !== undefined) {\n options.domain_hint = ctx.domainHint;\n }\n\n return ctx.helper.start(input, options);\n },\n\n async authenticate(input, ctx) {\n return ctx.helper.authenticate(input);\n },\n\n async refresh(input, ctx) {\n return ctx.helper.refresh(input);\n },\n});\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n OAuthAuthenticatorResult,\n createSignInResolverFactory,\n PassportProfile,\n SignInInfo,\n} from '@backstage/plugin-auth-node';\n\n/**\n * Available sign-in resolvers for the Microsoft auth provider.\n *\n * @public\n */\nexport namespace microsoftSignInResolvers {\n /**\n * Looks up the user by matching their Microsoft email to the email entity annotation.\n */\n export const emailMatchingUserEntityAnnotation = createSignInResolverFactory({\n create() {\n return async (\n info: SignInInfo<OAuthAuthenticatorResult<PassportProfile>>,\n ctx,\n ) => {\n const { profile } = info;\n\n if (!profile.email) {\n throw new Error('Microsoft profile contained no email');\n }\n\n return ctx.signInWithCatalogUser({\n annotations: {\n 'microsoft.com/email': profile.email,\n },\n });\n };\n },\n });\n /**\n * Looks up the user by matching their Microsoft user id to the user id entity annotation.\n */\n export const userIdMatchingUserEntityAnnotation = createSignInResolverFactory(\n {\n create() {\n return async (\n info: SignInInfo<OAuthAuthenticatorResult<PassportProfile>>,\n ctx,\n ) => {\n const { result } = info;\n\n const id = result.fullProfile.id;\n\n if (!id) {\n throw new Error('Microsoft profile contained no id');\n }\n\n return ctx.signInWithCatalogUser({\n annotations: {\n 'graph.microsoft.com/user-id': id,\n },\n });\n };\n },\n },\n );\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { createBackendModule } from '@backstage/backend-plugin-api';\nimport {\n authProvidersExtensionPoint,\n commonSignInResolvers,\n createOAuthProviderFactory,\n} from '@backstage/plugin-auth-node';\nimport { microsoftAuthenticator } from './authenticator';\nimport { microsoftSignInResolvers } from './resolvers';\n\n/** @public */\nexport const authModuleMicrosoftProvider = createBackendModule({\n pluginId: 'auth',\n moduleId: 'microsoft-provider',\n register(reg) {\n reg.registerInit({\n deps: {\n providers: authProvidersExtensionPoint,\n },\n async init({ providers }) {\n providers.registerProvider({\n providerId: 'microsoft',\n factory: createOAuthProviderFactory({\n authenticator: microsoftAuthenticator,\n signInResolverFactories: {\n ...microsoftSignInResolvers,\n ...commonSignInResolvers,\n },\n }),\n });\n },\n });\n },\n});\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { authModuleMicrosoftProvider as deprecatedAuthModuleMicrosoftProvider } from './module';\n\n/**\n * @public\n * @deprecated Use default import instead\n */\nexport const authModuleMicrosoftProvider =\n deprecatedAuthModuleMicrosoftProvider;\n"],"names":["MicrosoftStrategy","decodeJwt","fetch","createOAuthAuthenticator","PassportOAuthAuthenticatorHelper","microsoftSignInResolvers","createSignInResolverFactory","authModuleMicrosoftProvider","createBackendModule","authProvidersExtensionPoint","createOAuthProviderFactory","commonSignInResolvers","deprecatedAuthModuleMicrosoftProvider"],"mappings":";;;;;;;;;;;;;;AAqBO,MAAM,kCAAkCA,0BAAkB,CAAA;AAAA,EAC/D,WAAA,CACE,aACA,IACM,EAAA;AACN,IAAI,IAAA,IAAA,CAAK,eAAgB,CAAA,WAAW,CAAG,EAAA;AACrC,MAAA,IAAA,CAAK,MAAM,KAAS,CAAA,CAAA,CAAA;AACpB,MAAA,OAAA;AAAA,KACF;AAEA,IAAM,KAAA,CAAA,WAAA;AAAA,MACJ,WAAA;AAAA,MACA,CAAC,KAAe,OAA8B,KAAA;AAC5C,QAAI,IAAA,CAAC,OAAW,IAAA,OAAA,CAAQ,MAAQ,EAAA;AAC9B,UAAA,IAAA,CAAK,KAAK,OAAO,CAAA,CAAA;AACjB,UAAA,OAAA;AAAA,SACF;AAEA,QAAA,IAAA,CAAK,gBAAiB,CAAA,WAAW,CAAE,CAAA,IAAA,CAAK,CAAU,MAAA,KAAA;AAChD,UAAA,OAAA,CAAQ,MAAS,GAAA,MAAA,CAAA;AACjB,UAAA,IAAA,CAAK,KAAK,OAAO,CAAA,CAAA;AAAA,SAClB,CAAA,CAAA;AAAA,OACH;AAAA,KACF,CAAA;AAAA,GACF;AAAA,EAEQ,kBAAkB,WAA8B,EAAA;AACtD,IAAA,MAAM,EAAE,GAAA,EAAK,GAAI,EAAA,GAAIC,eAAU,WAAW,CAAA,CAAA;AAC1C,IAAA,OACE,GAAQ,KAAA,sCAAA,IACR,CAAC,CAAC,OACD,GACE,CAAA,KAAA,CAAM,GAAG,CAAA,CACT,IAAI,CAAK,CAAA,KAAA,CAAA,CAAE,iBAAkB,CAAA,OAAO,CAAC,CACrC,CAAA,IAAA;AAAA,MAAK,CACJ,CAAA,KAAA;AAAA,QACE,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AAC;;;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ var backendPluginApi = require('@backstage/backend-plugin-api');
4
+ var pluginAuthNode = require('@backstage/plugin-auth-node');
5
+ var authenticator = require('./authenticator.cjs.js');
6
+ var resolvers = require('./resolvers.cjs.js');
7
+
8
+ const authModuleMicrosoftProvider = backendPluginApi.createBackendModule({
9
+ pluginId: "auth",
10
+ moduleId: "microsoft-provider",
11
+ register(reg) {
12
+ reg.registerInit({
13
+ deps: {
14
+ providers: pluginAuthNode.authProvidersExtensionPoint
15
+ },
16
+ async init({ providers }) {
17
+ providers.registerProvider({
18
+ providerId: "microsoft",
19
+ factory: pluginAuthNode.createOAuthProviderFactory({
20
+ authenticator: authenticator.microsoftAuthenticator,
21
+ signInResolverFactories: {
22
+ ...resolvers.microsoftSignInResolvers,
23
+ ...pluginAuthNode.commonSignInResolvers
24
+ }
25
+ })
26
+ });
27
+ }
28
+ });
29
+ }
30
+ });
31
+
32
+ exports.authModuleMicrosoftProvider = authModuleMicrosoftProvider;
33
+ //# sourceMappingURL=module.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.cjs.js","sources":["../src/module.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { createBackendModule } from '@backstage/backend-plugin-api';\nimport {\n authProvidersExtensionPoint,\n commonSignInResolvers,\n createOAuthProviderFactory,\n} from '@backstage/plugin-auth-node';\nimport { microsoftAuthenticator } from './authenticator';\nimport { microsoftSignInResolvers } from './resolvers';\n\n/** @public */\nexport const authModuleMicrosoftProvider = createBackendModule({\n pluginId: 'auth',\n moduleId: 'microsoft-provider',\n register(reg) {\n reg.registerInit({\n deps: {\n providers: authProvidersExtensionPoint,\n },\n async init({ providers }) {\n providers.registerProvider({\n providerId: 'microsoft',\n factory: createOAuthProviderFactory({\n authenticator: microsoftAuthenticator,\n signInResolverFactories: {\n ...microsoftSignInResolvers,\n ...commonSignInResolvers,\n },\n }),\n });\n },\n });\n },\n});\n"],"names":["createBackendModule","authProvidersExtensionPoint","createOAuthProviderFactory","microsoftAuthenticator","microsoftSignInResolvers","commonSignInResolvers"],"mappings":";;;;;;;AAyBO,MAAM,8BAA8BA,oCAAoB,CAAA;AAAA,EAC7D,QAAU,EAAA,MAAA;AAAA,EACV,QAAU,EAAA,oBAAA;AAAA,EACV,SAAS,GAAK,EAAA;AACZ,IAAA,GAAA,CAAI,YAAa,CAAA;AAAA,MACf,IAAM,EAAA;AAAA,QACJ,SAAW,EAAAC,0CAAA;AAAA,OACb;AAAA,MACA,MAAM,IAAA,CAAK,EAAE,SAAA,EAAa,EAAA;AACxB,QAAA,SAAA,CAAU,gBAAiB,CAAA;AAAA,UACzB,UAAY,EAAA,WAAA;AAAA,UACZ,SAASC,yCAA2B,CAAA;AAAA,YAClC,aAAe,EAAAC,oCAAA;AAAA,YACf,uBAAyB,EAAA;AAAA,cACvB,GAAGC,kCAAA;AAAA,cACH,GAAGC,oCAAA;AAAA,aACL;AAAA,WACD,CAAA;AAAA,SACF,CAAA,CAAA;AAAA,OACH;AAAA,KACD,CAAA,CAAA;AAAA,GACH;AACF,CAAC;;;;"}
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ var pluginAuthNode = require('@backstage/plugin-auth-node');
4
+
5
+ exports.microsoftSignInResolvers = void 0;
6
+ ((microsoftSignInResolvers2) => {
7
+ microsoftSignInResolvers2.emailMatchingUserEntityAnnotation = pluginAuthNode.createSignInResolverFactory({
8
+ create() {
9
+ return async (info, ctx) => {
10
+ const { profile } = info;
11
+ if (!profile.email) {
12
+ throw new Error("Microsoft profile contained no email");
13
+ }
14
+ return ctx.signInWithCatalogUser({
15
+ annotations: {
16
+ "microsoft.com/email": profile.email
17
+ }
18
+ });
19
+ };
20
+ }
21
+ });
22
+ microsoftSignInResolvers2.userIdMatchingUserEntityAnnotation = pluginAuthNode.createSignInResolverFactory(
23
+ {
24
+ create() {
25
+ return async (info, ctx) => {
26
+ const { result } = info;
27
+ const id = result.fullProfile.id;
28
+ if (!id) {
29
+ throw new Error("Microsoft profile contained no id");
30
+ }
31
+ return ctx.signInWithCatalogUser({
32
+ annotations: {
33
+ "graph.microsoft.com/user-id": id
34
+ }
35
+ });
36
+ };
37
+ }
38
+ }
39
+ );
40
+ })(exports.microsoftSignInResolvers || (exports.microsoftSignInResolvers = {}));
41
+ //# sourceMappingURL=resolvers.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolvers.cjs.js","sources":["../src/resolvers.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n OAuthAuthenticatorResult,\n createSignInResolverFactory,\n PassportProfile,\n SignInInfo,\n} from '@backstage/plugin-auth-node';\n\n/**\n * Available sign-in resolvers for the Microsoft auth provider.\n *\n * @public\n */\nexport namespace microsoftSignInResolvers {\n /**\n * Looks up the user by matching their Microsoft email to the email entity annotation.\n */\n export const emailMatchingUserEntityAnnotation = createSignInResolverFactory({\n create() {\n return async (\n info: SignInInfo<OAuthAuthenticatorResult<PassportProfile>>,\n ctx,\n ) => {\n const { profile } = info;\n\n if (!profile.email) {\n throw new Error('Microsoft profile contained no email');\n }\n\n return ctx.signInWithCatalogUser({\n annotations: {\n 'microsoft.com/email': profile.email,\n },\n });\n };\n },\n });\n /**\n * Looks up the user by matching their Microsoft user id to the user id entity annotation.\n */\n export const userIdMatchingUserEntityAnnotation = createSignInResolverFactory(\n {\n create() {\n return async (\n info: SignInInfo<OAuthAuthenticatorResult<PassportProfile>>,\n ctx,\n ) => {\n const { result } = info;\n\n const id = result.fullProfile.id;\n\n if (!id) {\n throw new Error('Microsoft profile contained no id');\n }\n\n return ctx.signInWithCatalogUser({\n annotations: {\n 'graph.microsoft.com/user-id': id,\n },\n });\n };\n },\n },\n );\n}\n"],"names":["microsoftSignInResolvers","createSignInResolverFactory"],"mappings":";;;;AA4BiBA,0CAAA;AAAA,CAAV,CAAUA,yBAAV,KAAA;AAIE,EAAMA,yBAAAA,CAAA,oCAAoCC,0CAA4B,CAAA;AAAA,IAC3E,MAAS,GAAA;AACP,MAAO,OAAA,OACL,MACA,GACG,KAAA;AACH,QAAM,MAAA,EAAE,SAAY,GAAA,IAAA,CAAA;AAEpB,QAAI,IAAA,CAAC,QAAQ,KAAO,EAAA;AAClB,UAAM,MAAA,IAAI,MAAM,sCAAsC,CAAA,CAAA;AAAA,SACxD;AAEA,QAAA,OAAO,IAAI,qBAAsB,CAAA;AAAA,UAC/B,WAAa,EAAA;AAAA,YACX,uBAAuB,OAAQ,CAAA,KAAA;AAAA,WACjC;AAAA,SACD,CAAA,CAAA;AAAA,OACH,CAAA;AAAA,KACF;AAAA,GACD,CAAA,CAAA;AAIM,EAAMD,0BAAA,kCAAqC,GAAAC,0CAAA;AAAA,IAChD;AAAA,MACE,MAAS,GAAA;AACP,QAAO,OAAA,OACL,MACA,GACG,KAAA;AACH,UAAM,MAAA,EAAE,QAAW,GAAA,IAAA,CAAA;AAEnB,UAAM,MAAA,EAAA,GAAK,OAAO,WAAY,CAAA,EAAA,CAAA;AAE9B,UAAA,IAAI,CAAC,EAAI,EAAA;AACP,YAAM,MAAA,IAAI,MAAM,mCAAmC,CAAA,CAAA;AAAA,WACrD;AAEA,UAAA,OAAO,IAAI,qBAAsB,CAAA;AAAA,YAC/B,WAAa,EAAA;AAAA,cACX,6BAA+B,EAAA,EAAA;AAAA,aACjC;AAAA,WACD,CAAA,CAAA;AAAA,SACH,CAAA;AAAA,OACF;AAAA,KACF;AAAA,GACF,CAAA;AAAA,CAlDe,EAAAD,gCAAA,KAAAA,gCAAA,GAAA,EAAA,CAAA,CAAA;;"}
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ var jose = require('jose');
4
+ var fetch = require('node-fetch');
5
+ var passportMicrosoft = require('passport-microsoft');
6
+
7
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
8
+
9
+ var fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
10
+
11
+ class ExtendedMicrosoftStrategy extends passportMicrosoft.Strategy {
12
+ shouldSkipUserProfile = false;
13
+ setSkipUserProfile(shouldSkipUserProfile) {
14
+ this.shouldSkipUserProfile = shouldSkipUserProfile;
15
+ }
16
+ userProfile(accessToken, done) {
17
+ if (this.skipUserProfile(accessToken)) {
18
+ done(null, void 0);
19
+ return;
20
+ }
21
+ super.userProfile(
22
+ accessToken,
23
+ (err, profile) => {
24
+ if (!profile || profile.photos) {
25
+ done(err, profile);
26
+ return;
27
+ }
28
+ this.getProfilePhotos(accessToken).then((photos) => {
29
+ profile.photos = photos;
30
+ done(err, profile);
31
+ });
32
+ }
33
+ );
34
+ }
35
+ hasGraphReadScope(accessToken) {
36
+ const { aud, scp } = jose.decodeJwt(accessToken);
37
+ return aud === "00000003-0000-0000-c000-000000000000" && !!scp && scp.split(" ").map((s) => s.toLocaleLowerCase("en-US")).some(
38
+ (s) => [
39
+ "https://graph.microsoft.com/user.read",
40
+ "https://graph.microsoft.com/user.read.all",
41
+ "user.read",
42
+ "user.read.all"
43
+ ].includes(s)
44
+ );
45
+ }
46
+ skipUserProfile(accessToken) {
47
+ try {
48
+ return this.shouldSkipUserProfile || !this.hasGraphReadScope(accessToken);
49
+ } catch {
50
+ return false;
51
+ }
52
+ }
53
+ async getProfilePhotos(accessToken) {
54
+ return this.getCurrentUserPhoto(accessToken, "96x96").then(
55
+ (photo) => photo ? [{ value: photo }] : void 0
56
+ );
57
+ }
58
+ async getCurrentUserPhoto(accessToken, size) {
59
+ try {
60
+ const res = await fetch__default.default(
61
+ `https://graph.microsoft.com/v1.0/me/photos/${size}/$value`,
62
+ {
63
+ headers: {
64
+ Authorization: `Bearer ${accessToken}`
65
+ }
66
+ }
67
+ );
68
+ const data = await res.buffer();
69
+ return `data:image/jpeg;base64,${data.toString("base64")}`;
70
+ } catch (error) {
71
+ return void 0;
72
+ }
73
+ }
74
+ }
75
+
76
+ exports.ExtendedMicrosoftStrategy = ExtendedMicrosoftStrategy;
77
+ //# sourceMappingURL=strategy.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy.cjs.js","sources":["../src/strategy.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PassportProfile } from '@backstage/plugin-auth-node';\nimport { decodeJwt } from 'jose';\nimport fetch from 'node-fetch';\nimport { Strategy as MicrosoftStrategy } from 'passport-microsoft';\n\nexport class ExtendedMicrosoftStrategy extends MicrosoftStrategy {\n private shouldSkipUserProfile = false;\n\n public setSkipUserProfile(shouldSkipUserProfile: boolean): void {\n this.shouldSkipUserProfile = shouldSkipUserProfile;\n }\n\n userProfile(\n accessToken: string,\n done: (err?: unknown, profile?: PassportProfile) => void,\n ): void {\n if (this.skipUserProfile(accessToken)) {\n done(null, undefined);\n return;\n }\n\n super.userProfile(\n accessToken,\n (err?: unknown, profile?: PassportProfile) => {\n if (!profile || profile.photos) {\n done(err, profile);\n return;\n }\n\n this.getProfilePhotos(accessToken).then(photos => {\n profile.photos = photos;\n done(err, profile);\n });\n },\n );\n }\n\n private hasGraphReadScope(accessToken: string): boolean {\n const { aud, scp } = decodeJwt(accessToken);\n return (\n aud === '00000003-0000-0000-c000-000000000000' &&\n !!scp &&\n (scp as string)\n .split(' ')\n .map(s => s.toLocaleLowerCase('en-US'))\n .some(s =>\n [\n 'https://graph.microsoft.com/user.read',\n 'https://graph.microsoft.com/user.read.all',\n 'user.read',\n 'user.read.all',\n ].includes(s),\n )\n );\n }\n\n private skipUserProfile(accessToken: string): boolean {\n try {\n return this.shouldSkipUserProfile || !this.hasGraphReadScope(accessToken);\n } catch {\n // If there is any error with checking the scope\n // we fall back to not skipping the user profile\n // which may still result in an auth failure\n // e.g. due to a foreign scope.\n return false;\n }\n }\n\n private async getProfilePhotos(\n accessToken: string,\n ): Promise<Array<{ value: string }> | undefined> {\n return this.getCurrentUserPhoto(accessToken, '96x96').then(photo =>\n photo ? [{ value: photo }] : undefined,\n );\n }\n\n private async getCurrentUserPhoto(\n accessToken: string,\n size: string,\n ): Promise<string | undefined> {\n try {\n const res = await fetch(\n `https://graph.microsoft.com/v1.0/me/photos/${size}/$value`,\n {\n headers: {\n Authorization: `Bearer ${accessToken}`,\n },\n },\n );\n const data = await res.buffer();\n\n return `data:image/jpeg;base64,${data.toString('base64')}`;\n } catch (error) {\n return undefined;\n }\n }\n}\n"],"names":["MicrosoftStrategy","decodeJwt","fetch"],"mappings":";;;;;;;;;;AAqBO,MAAM,kCAAkCA,0BAAkB,CAAA;AAAA,EACvD,qBAAwB,GAAA,KAAA,CAAA;AAAA,EAEzB,mBAAmB,qBAAsC,EAAA;AAC9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MAAA,OAAO,IAAK,CAAA,qBAAA,IAAyB,CAAC,IAAA,CAAK,kBAAkB,WAAW,CAAA,CAAA;AAAA,KAClE,CAAA,MAAA;AAKN,MAAO,OAAA,KAAA,CAAA;AAAA,KACT;AAAA,GACF;AAAA,EAEA,MAAc,iBACZ,WAC+C,EAAA;AAC/C,IAAA,OAAO,IAAK,CAAA,mBAAA,CAAoB,WAAa,EAAA,OAAO,CAAE,CAAA,IAAA;AAAA,MAAK,WACzD,KAAQ,GAAA,CAAC,EAAE,KAAO,EAAA,KAAA,EAAO,CAAI,GAAA,KAAA,CAAA;AAAA,KAC/B,CAAA;AAAA,GACF;AAAA,EAEA,MAAc,mBACZ,CAAA,WAAA,EACA,IAC6B,EAAA;AAC7B,IAAI,IAAA;AACF,MAAA,MAAM,MAAM,MAAMC,sBAAA;AAAA,QAChB,8CAA8C,IAAI,CAAA,OAAA,CAAA;AAAA,QAClD;AAAA,UACE,OAAS,EAAA;AAAA,YACP,aAAA,EAAe,UAAU,WAAW,CAAA,CAAA;AAAA,WACtC;AAAA,SACF;AAAA,OACF,CAAA;AACA,MAAM,MAAA,IAAA,GAAO,MAAM,GAAA,CAAI,MAAO,EAAA,CAAA;AAE9B,MAAA,OAAO,CAA0B,uBAAA,EAAA,IAAA,CAAK,QAAS,CAAA,QAAQ,CAAC,CAAA,CAAA,CAAA;AAAA,aACjD,KAAO,EAAA;AACd,MAAO,OAAA,KAAA,CAAA,CAAA;AAAA,KACT;AAAA,GACF;AACF;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-auth-backend-module-microsoft-provider",
3
- "version": "0.2.0",
3
+ "version": "0.2.1-next.1",
4
4
  "description": "The microsoft-provider backend module for the auth plugin.",
5
5
  "backstage": {
6
6
  "role": "backend-plugin-module",
@@ -34,19 +34,19 @@
34
34
  "test": "backstage-cli package test"
35
35
  },
36
36
  "dependencies": {
37
- "@backstage/backend-plugin-api": "^1.0.0",
38
- "@backstage/plugin-auth-node": "^0.5.2",
37
+ "@backstage/backend-plugin-api": "1.0.1-next.1",
38
+ "@backstage/plugin-auth-node": "0.5.3-next.1",
39
39
  "express": "^4.18.2",
40
40
  "jose": "^5.0.0",
41
41
  "node-fetch": "^2.7.0",
42
42
  "passport-microsoft": "^1.0.0"
43
43
  },
44
44
  "devDependencies": {
45
- "@backstage/backend-defaults": "^0.5.0",
46
- "@backstage/backend-test-utils": "^1.0.0",
47
- "@backstage/cli": "^0.27.1",
48
- "@backstage/config": "^1.2.0",
49
- "@backstage/plugin-auth-backend": "^0.23.0",
45
+ "@backstage/backend-defaults": "0.5.1-next.2",
46
+ "@backstage/backend-test-utils": "1.0.1-next.2",
47
+ "@backstage/cli": "0.28.0-next.2",
48
+ "@backstage/config": "1.2.0",
49
+ "@backstage/plugin-auth-backend": "0.23.1-next.1",
50
50
  "@types/passport-microsoft": "^1.0.0",
51
51
  "msw": "^1.0.0",
52
52
  "supertest": "^7.0.0"