UNPKG

3.89 kBJavaScriptView Raw
1/****************
2 * IMPORTS
3 */
4
5var util = require('util')
6var OAuth2Strategy = require('passport-oauth2')
7var InternalOAuthError = require('passport-oauth2').InternalOAuthError
8
9/**
10 * `Strategy` constructor.
11 *
12 * The Microsoft authentication strategy authenticates requests by delegating to
13 * Microsoft using the OAuth 2.0 protocol.
14 *
15 * Applications must supply a `verify` callback which accepts an `accessToken`,
16 * `refreshToken` and service-specific `profile`, and then calls the `done`
17 * callback supplying a `user`, which should be set to `false` if the
18 * credentials are not valid. If an exception occured, `err` should be set.
19 *
20 * Options:
21 * - `clientId` your Microsoft application's client id
22 * - `clientSecret` your Microsoft application's client secret
23 * - `callbackURL` URL to which Microsoft will redirect the user after granting authorization in your Microsoft Application
24 *
25 * Examples:
26 *
27 * var MicrosoftStrategy = require('passport-microsoft').Strategy;
28 *
29 * passport.use(new MicrosoftStrategy({
30 * clientID: '123-456-789',
31 * clientSecret: 'shhh-its-a-secret'
32 * callbackURL: 'https://www.example.net/auth/microsoft/callback'
33 * },
34 * function(accessToken, refreshToken, profile, done) {
35 * User.findOrCreate(..., function (err, user) {
36 * done(err, user);
37 * });
38 * }
39 * ));
40 *
41 * @param {Object} options
42 * @param {Function} verify
43 * @api public
44 */
45
46function MicrosoftStrategy(options, verify) {
47 options = options || {}
48 options.authorizationURL = options.authorizationURL || 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
49 options.tokenURL = options.tokenURL || 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
50 options.scopeSeparator = options.scopeSeparator || ' ';
51 options.customHeaders = options.customHeaders || {};
52
53 OAuth2Strategy.call(this, options, verify)
54 this.name = 'microsoft'
55}
56
57/**
58 * Inherit from `OAuth2Strategy`.
59 */
60
61util.inherits(MicrosoftStrategy, OAuth2Strategy)
62
63/**
64 * Allow prompt and other params.
65 */
66
67MicrosoftStrategy.prototype.authorizationParams = function(options) {
68 var params = {};
69
70 ['locale', 'display'].forEach(function(name) {
71 if (options[name]) {
72 params[name] = options[name]
73 }
74 });
75
76 if (options.prompt) {
77 params['prompt'] = options.prompt;
78 }
79
80 return params;
81 };
82
83/**
84 * Retrieve user profile from Microsoft Graph.
85 *
86 * This function constructs a normalized profile, with the following properties:
87 *
88 * - `provider` always set to `microsoft`
89 * - `id`
90 * - etc..
91 *
92 * @param {String} accessToken
93 * @param {Function} done
94 * @api protected
95 */
96
97MicrosoftStrategy.prototype.userProfile = function (accessToken, done) {
98
99 this._oauth2.useAuthorizationHeaderforGET(true);
100 this._oauth2.get(
101 'https://graph.microsoft.com/v1.0/me/',
102 accessToken,
103 function (err, body, res) {
104
105 if (err) {
106 return done(new InternalOAuthError('failed to fetch user profile', err))
107 }
108 try {
109 var json = JSON.parse(body)
110
111 var profile = {
112 provider: 'microsoft',
113 name: {}
114 }
115 profile.id = json.id
116 profile.displayName = json.displayName
117 profile.name.familyName = json.surname
118 profile.name.givenName = json.givenName
119 profile.emails = [{ type: 'work', value: json.mail || json.userPrincipalName }]
120
121 profile._raw = body
122 profile._json = json
123
124 done(null, profile)
125 }
126 catch (e) {
127 done(e)
128 }
129 }
130 )
131}
132
133/**
134 * Expose `Strategy`.
135 */
136
137module.exports = MicrosoftStrategy