@@ -10,7 +10,7 @@ const url = require('url')
10
10
const db = require ( '../db' )
11
11
const xml2js = require ( 'xml2js' )
12
12
const InternalOAuthError = require ( 'passport-oauth' ) . InternalOAuthError
13
- const OSMStrategy = require ( 'passport-openstreetmap ' ) . Strategy
13
+ const OAuth2Strategy = require ( 'passport-oauth2 ' ) . Strategy
14
14
15
15
const { serverRuntimeConfig, publicRuntimeConfig } = require ( '../../next.config' )
16
16
@@ -23,8 +23,9 @@ function openstreetmap (req, res) {
23
23
/**
24
24
* override the userProfile method of OSMStrategy to allow for custom osm endpoints
25
25
*/
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 ) {
28
29
if ( err ) { return done ( new InternalOAuthError ( 'failed to fetch user profile' , err ) ) }
29
30
30
31
var parser = new xml2js . Parser ( )
@@ -45,31 +46,31 @@ function openstreetmap (req, res) {
45
46
} )
46
47
}
47
48
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 ,
54
55
callbackURL : `${ publicRuntimeConfig . APP_URL } /oauth/openstreetmap/callback?login_challenge=${ encodeURIComponent ( challenge ) } `
55
- } , async ( token , tokenSecret , profile , done ) => {
56
+ } , async ( accessToken , refreshToken , profile , done ) => {
56
57
let conn = await db ( )
57
58
let [ user ] = await conn ( 'users' ) . where ( 'id' , profile . id )
58
59
if ( user ) {
59
60
const newProfile = R . mergeDeepRight ( user . profile , profile )
60
61
await conn ( 'users' ) . where ( 'id' , profile . id ) . update (
61
62
{
62
- 'osmToken' : token ,
63
- 'osmTokenSecret' : tokenSecret ,
63
+ 'osmToken' : accessToken ,
64
+ 'osmTokenSecret' : refreshToken ,
64
65
'profile' : JSON . stringify ( newProfile )
65
66
}
66
67
)
67
68
} else {
68
69
await conn ( 'users' ) . insert (
69
70
{
70
71
'id' : profile . id ,
71
- 'osmToken' : token ,
72
- 'osmTokenSecret' : tokenSecret ,
72
+ 'osmToken' : accessToken ,
73
+ 'osmTokenSecret' : refreshToken ,
73
74
profile : JSON . stringify ( profile )
74
75
}
75
76
)
0 commit comments