-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init global api-client with auto-refresh token, loggedInUserService and some minor fixes
- Loading branch information
Showing
29 changed files
with
501 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports.AuthFailed = class AuthFailed extends Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { Axios } = require('axios') | ||
const refresh = require('passport-oauth2-refresh') | ||
const { AuthFailed } = require('./ApiErrors') | ||
const appConfig = require('../config/app') | ||
|
||
const getRefreshToken = (oAuthPassport) => { | ||
return new Promise((resolve, reject) => { | ||
refresh.requestNewAccessToken(appConfig.oauth.strategy, oAuthPassport.refreshToken, function (err, accessToken, refreshToken) { | ||
if (err || !accessToken || !refreshToken) { | ||
return reject(new AuthFailed('Failed to refresh token')) | ||
} | ||
|
||
return resolve([accessToken, refreshToken]) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports.JavaApiClientFactory = (javaApiBaseURL, oAuthPassport) => { | ||
if (typeof oAuthPassport !== "object") { | ||
throw new Error("oAuthPassport not an object"); | ||
} | ||
|
||
if (typeof oAuthPassport.refreshToken !== "string") { | ||
throw new Error("oAuthPassport.refreshToken not a string") | ||
} | ||
|
||
if (typeof oAuthPassport.token !== "string") { | ||
throw new Error("oAuthPassport.token not a string") | ||
} | ||
|
||
let tokenRefreshRunning = null | ||
const client = new Axios({ | ||
baseURL: javaApiBaseURL | ||
}) | ||
|
||
client.interceptors.request.use( | ||
async config => { | ||
config.headers = { | ||
Authorization: `Bearer ${oAuthPassport.token}` | ||
} | ||
|
||
return config | ||
}) | ||
|
||
client.interceptors.response.use((res) => { | ||
if (!res.config._refreshTokenRequest && res.config && res.status === 401) { | ||
res.config._refreshTokenRequest = true | ||
|
||
if (!tokenRefreshRunning) { | ||
tokenRefreshRunning = getRefreshToken(oAuthPassport) | ||
} | ||
|
||
return tokenRefreshRunning.then(([token, refreshToken]) => { | ||
oAuthPassport.token = token | ||
oAuthPassport.refreshToken = refreshToken | ||
|
||
return client.request(res.config) | ||
}) | ||
} | ||
|
||
if (res.status === 401) { | ||
throw new AuthFailed('Token no longer valid and refresh did not help') | ||
} | ||
|
||
return res | ||
}) | ||
|
||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class LoggedInUserService { | ||
constructor (userRepository, request) { | ||
this.request = request | ||
|
||
if (typeof this.request.user !== "object") { | ||
throw new Error("request.user not an object"); | ||
} | ||
} | ||
|
||
getUser() { | ||
return this.request.user | ||
} | ||
} | ||
|
||
module.exports = LoggedInUserService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class UserRepository { | ||
constructor (javaApiClient) { | ||
this.javaApiClient = javaApiClient | ||
} | ||
|
||
fetchUser (oAuthPassport) { | ||
return this.javaApiClient.get('/me').then(response => { | ||
const rawUser = JSON.parse(response.data).data | ||
|
||
let clan = null | ||
|
||
if (rawUser.attributes.clan) { | ||
clan = { | ||
id: rawUser.attributes.clan.id, | ||
membershipId: rawUser.attributes.clan.membershipId, | ||
tag: rawUser.attributes.clan.tag, | ||
name: rawUser.attributes.clan.name | ||
} | ||
} | ||
|
||
return { | ||
id: rawUser.id, | ||
name: rawUser.attributes.userName, | ||
email: rawUser.attributes.email, | ||
clan, | ||
oAuthPassport | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = UserRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.