Skip to content

Commit

Permalink
Merge pull request #18 from Lakhan-Nad/master
Browse files Browse the repository at this point in the history
Removed request library dependency by using _oauth.request method for making _oauth.get calls
  • Loading branch information
clocked0ne authored Mar 28, 2021
2 parents c37149d + 260164a commit 471ce64
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 26 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
language: "node_js"
node_js:
- "6.0"
- "5.0"
- "4.0"
- "0.12"
- "15"
- "14"
- "13"
- "12"
- "11"
- "10"

before_install:
- "npm install [email protected] -g"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ $ npm install --save passport-outlook

## Usage

#### v3

There are no behavioural changes but as of v3 the minimum required NodeJS version
is v10. This should not affect most users but is a breaking change nonetheless.

#### Upgrading for v2

If you were using the package before `v2.0.0`, please note that the `profile`
Expand Down
61 changes: 61 additions & 0 deletions examples/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const passport = require("passport");
const OutlookStrategy = require("../lib/strategy");
require("dotenv").config();

passport.use(
new OutlookStrategy(
{
callbackURL: "http://localhost:3000/auth/outlook/redirect",
passReqToCallback: true,
clientID: "your-client-id",
clientSecret: "your-client-secret",
userProfileURL: "https://graph.microsoft.com/v1.0/me/",
},
(req, accessToken, refreshToken, profile, done) => {
if (profile._json.id) {
let user = {
auth_id: profile._json.id,
email: profile._json.userPrincipalName,
pic_link: "#",
};
console.log(profile);
done(null, user);
} else {
done(null, null, { message: "Invalid Authentication" });
}
}
)
);

const app = require("express")();

app.use(passport.initialize());

app.get("/home", (req, res) => {
res.send("Home");
});

app.get("/login", (req, res) => {
res.send("Login");
});

app.get(
"/auth/outlook/redirect/",
passport.authenticate("windowslive", {
failureRedirect: "/login",
successRedirect: "/home",
session: false,
})
);

app.get(
"/auth/outlook",
passport.authenticate("windowslive", {
scope: ["user.read", "User.Read.All"],
prompt: "select_account",
})
);

app.listen(3000, "localhost", () => {
console.log("server started");
});
25 changes: 12 additions & 13 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ var util = require('util')
, OAuth2Strategy = require('passport-oauth2')
, Profile = require('./profile')
, InternalOAuthError = require('passport-oauth2').InternalOAuthError
, OutlookAPIError = require('./errors/outlookapierror')
, request = require('request');
, OutlookAPIError = require('./errors/outlookapierror');


/**
Expand Down Expand Up @@ -53,17 +52,17 @@ function Strategy (options, verify) {
this._userProfileURL = options.userProfileURL || 'https://outlook.office.com/api/v2.0/me';

/**
* Overwrite `_oauth2.get` to use `request.get` allowing for custom headers.
* We can overwrite _oauth2.get and use _oauth2.request to make the get request
* which even _oauth2.get uses internally
*
*/
this._oauth2.get = function (url, accessToken, callback) {
request.get({
url: url,
headers: {
'Authorization': 'Bearer ' + accessToken,
'Accept': 'application/json; odata.metadata=none'
}
}, callback);
};
this._oauth2.get = function (url, access_token, callback) {
var headers = {
'Authorization': 'Bearer ' + access_token,
'Accept': 'application/json; odata.metadata=none'
}
this._request("GET", url, headers, "", access_token, callback);
}

}

Expand Down Expand Up @@ -122,7 +121,7 @@ Strategy.prototype.authorizationParams = function (options) {
* @api protected
*/
Strategy.prototype.userProfile = function (accessToken, done) {
this._oauth2.get(this._userProfileURL, accessToken, function (err, res, body) {
this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) {
/* Specific case where a user creates a new Outlook account via the OAuth screen
* and the redirect back to the application bypasses M$'s account finalisation
* process, giving you an Outlook account with no Mailbox entry.
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passport-outlook",
"version": "2.3.0",
"version": "3.0.0",
"description": "Outlook REST API v2 authentication strategy for Passport.",
"keywords": [
"passport",
Expand Down Expand Up @@ -38,6 +38,10 @@
{
"name": "Dan Perry",
"url": "https://github.com/dperry"
},
{
"name": "Lakhan Nad",
"url": "https://github.com/Lakhan-Nad"
}
],
"repository": {
Expand All @@ -55,17 +59,16 @@
],
"main": "./lib",
"dependencies": {
"passport-oauth2": "1.x.x",
"request": "2.x.x"
"passport-oauth2": "1.x.x"
},
"devDependencies": {
"make-node": "0.3.x",
"mocha": "3.x.x",
"chai": "3.x.x",
"make-node": "0.4.x",
"mocha": "8.x.x",
"chai": "4.x.x",
"chai-passport-strategy": "1.x.x"
},
"engines": {
"node": ">= 0.12.0"
"node": ">= 10.0.0"
},
"scripts": {
"test": "node_modules/.bin/mocha --reporter spec --require test/bootstrap/node test/*.test.js"
Expand Down
2 changes: 1 addition & 1 deletion test/strategy.profile.error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Strategy#userProfile', function() {
}
};

callback(null, res, undefined);
callback(null, undefined, res);
};

var err, profile;
Expand Down
2 changes: 1 addition & 1 deletion test/strategy.profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Strategy#userProfile', function() {
"MailboxGuid": "8d899a1e-bde4-4946-8817-005e6f11d36d" \
}';

callback(null, undefined, body);
callback(null, body);
};


Expand Down

0 comments on commit 471ce64

Please sign in to comment.