Skip to content

Commit b2e4bfe

Browse files
committed
change passport strategy to support oauth2
1 parent af04576 commit b2e4bfe

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**

app/lib/osm.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const url = require('url')
1010
const db = require('../db')
1111
const xml2js = require('xml2js')
1212
const InternalOAuthError = require('passport-oauth').InternalOAuthError
13-
const OSMStrategy = require('passport-openstreetmap').Strategy
13+
const OAuth2Strategy = require('passport-oauth2').Strategy
1414

1515
const { serverRuntimeConfig, publicRuntimeConfig } = require('../../next.config')
1616

@@ -23,8 +23,9 @@ function openstreetmap (req, res) {
2323
/**
2424
* override the userProfile method of OSMStrategy to allow for custom osm endpoints
2525
*/
26-
OSMStrategy.prototype.userProfile = function (token, tokenSecret, params, done) {
27-
this._oauth.get(`${OSM_API}/api/0.6/user/details`, token, tokenSecret, function (err, body, res) {
26+
OAuth2Strategy.prototype.userProfile = function (accessToken, done) {
27+
this._oauth2.useAuthorizationHeaderforGET(true)
28+
this._oauth2.get(`${OSM_API}/api/0.6/user/details`, accessToken, function (err, body, res) {
2829
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)) }
2930

3031
var parser = new xml2js.Parser()
@@ -45,31 +46,31 @@ function openstreetmap (req, res) {
4546
})
4647
}
4748

48-
const strategy = new OSMStrategy({
49-
requestTokenURL: `${OSM_API}/oauth/request_token`,
50-
accessTokenURL: `${OSM_API}/oauth/access_token`,
51-
userAuthorizationURL: `${OSM_DOMAIN}/oauth/authorize`,
52-
consumerKey: OSM_CONSUMER_KEY,
53-
consumerSecret: OSM_CONSUMER_SECRET,
49+
const strategy = new OAuth2Strategy({
50+
authorizationURL: `${OSM_DOMAIN}/oauth2/authorize`,
51+
tokenURL: `${OSM_DOMAIN}/oauth2/token`,
52+
clientID: OSM_CONSUMER_KEY,
53+
scope: ['openid', 'read_prefs'],
54+
clientSecret: OSM_CONSUMER_SECRET,
5455
callbackURL: `${publicRuntimeConfig.APP_URL}/oauth/openstreetmap/callback?login_challenge=${encodeURIComponent(challenge)}`
55-
}, async (token, tokenSecret, profile, done) => {
56+
}, async (accessToken, refreshToken, profile, done) => {
5657
let conn = await db()
5758
let [user] = await conn('users').where('id', profile.id)
5859
if (user) {
5960
const newProfile = R.mergeDeepRight(user.profile, profile)
6061
await conn('users').where('id', profile.id).update(
6162
{
62-
'osmToken': token,
63-
'osmTokenSecret': tokenSecret,
63+
'osmToken': accessToken,
64+
'osmTokenSecret': refreshToken,
6465
'profile': JSON.stringify(newProfile)
6566
}
6667
)
6768
} else {
6869
await conn('users').insert(
6970
{
7071
'id': profile.id,
71-
'osmToken': token,
72-
'osmTokenSecret': tokenSecret,
72+
'osmToken': accessToken,
73+
'osmTokenSecret': refreshToken,
7374
profile: JSON.stringify(profile)
7475
}
7576
)

next.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
serverRuntimeConfig: {
99
NODE_ENV: process.env.NODE_ENV || 'development',
1010
OSM_DOMAIN: process.env.OSM_DOMAIN || 'https://www.openstreetmap.org',
11-
OSM_API: process.env.OSM_API || process.env.OSM_DOMAIN || 'https://www.openstreetmap.org',
11+
OSM_API: process.env.OSM_API || process.env.OSM_DOMAIN || 'https://api.openstreetmap.org',
1212
OSM_HYDRA_ID: process.env.OSM_HYDRA_ID || 'manage',
1313
OSM_HYDRA_SECRET: process.env.OSM_HYDRA_SECRET || 'manage-secret',
1414
OSM_CONSUMER_KEY: process.env.OSM_CONSUMER_KEY,
@@ -20,7 +20,7 @@ module.exports = {
2020
HYDRA_ADMIN_HOST: process.env.HYDRA_ADMIN_HOST || 'http://localhost:4445'
2121
},
2222
publicRuntimeConfig: {
23-
APP_URL: process.env.APP_URL || 'http://localhost:8989',
23+
APP_URL: process.env.APP_URL || 'http://127.0.0.1:8989',
2424
OSM_NAME: process.env.OSM_NAME || 'OSM'
2525
},
2626
onDemandEntries: {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
]
4242
},
4343
"dependencies": {
44-
"body-parser": "^1.19.1",
44+
"body-parser": "^1.20.2",
4545
"chance": "^1.1.8",
4646
"compression": "^1.7.3",
4747
"connect-session-knex": "^2.1.1",
@@ -65,6 +65,7 @@
6565
"node-fetch": "^2.6.7",
6666
"passport-light": "^1.0.1",
6767
"passport-oauth": "^1.0.0",
68+
"passport-oauth2": "^1.8.0",
6869
"passport-openstreetmap": "^0.1.2",
6970
"pg": "^8.7.1",
7071
"pino": "^5.17.0",

0 commit comments

Comments
 (0)