remix-auth-microsoft
Version:
The Microsoft strategy is used to authenticate users against an account on [Microsoft Active Directory](https://docs.microsoft.com/en-us/azure/active-directory/develop/) using [Remix Auth](https://github.com/sergiodxa/remix-auth). This can be a work/schoo
64 lines (63 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MicrosoftStrategy = exports.MicrosoftStrategyScopeSeperator = exports.MicrosoftStrategyDefaultName = exports.MicrosoftStrategyDefaultScopes = void 0;
const remix_auth_oauth2_1 = require("remix-auth-oauth2");
exports.MicrosoftStrategyDefaultScopes = [
"openid",
"profile",
"email",
];
exports.MicrosoftStrategyDefaultName = "microsoft";
exports.MicrosoftStrategyScopeSeperator = " ";
class MicrosoftStrategy extends remix_auth_oauth2_1.OAuth2Strategy {
constructor({ clientId, clientSecret, redirectUri, scope, prompt, tenantId = "common", }, verify) {
super({
clientID: clientId,
clientSecret,
callbackURL: redirectUri,
authorizationURL: `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/authorize`,
tokenURL: `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
}, verify);
this.name = exports.MicrosoftStrategyDefaultName;
this.userInfoURL = "https://graph.microsoft.com/oidc/userinfo";
this.scope = this.getScope(scope);
this.prompt = prompt !== null && prompt !== void 0 ? prompt : "none";
}
//Allow users the option to pass a scope string, or typed array
getScope(scope) {
if (!scope) {
return exports.MicrosoftStrategyDefaultScopes.join(exports.MicrosoftStrategyScopeSeperator);
}
else if (typeof scope === "string") {
return scope;
}
return scope.join(exports.MicrosoftStrategyScopeSeperator);
}
authorizationParams() {
return new URLSearchParams({
scope: this.scope,
prompt: this.prompt,
});
}
async userProfile(accessToken) {
const response = await fetch(this.userInfoURL, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const data = await response.json();
const profile = {
provider: exports.MicrosoftStrategyDefaultName,
displayName: data.name,
id: data.sub,
name: {
familyName: data.family_name,
givenName: data.given_name,
},
emails: [{ value: data.email }],
_json: data,
};
return profile;
}
}
exports.MicrosoftStrategy = MicrosoftStrategy;