-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from olinlibrary/osteele/oauth
Enable OAuth sign-in
- Loading branch information
Showing
5 changed files
with
91 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { removeOauthParams } from '../data/auth'; | ||
|
||
describe('removeOauthParams', () => { | ||
test('removes parameters', () => { | ||
expect(removeOauthParams('http://localhost/path')).toEqual('http://localhost/path'); | ||
expect(removeOauthParams('http://localhost/path?access_token=value')).toEqual('http://localhost/path'); | ||
expect(removeOauthParams('http://localhost/path?expires_in=value')).toEqual('http://localhost/path'); | ||
expect(removeOauthParams('http://localhost/path?state=value')).toEqual('http://localhost/path'); | ||
expect(removeOauthParams('http://localhost/path?token_type=value')).toEqual('http://localhost/path'); | ||
expect(removeOauthParams('http://localhost/path?other_param=value')).toEqual('http://localhost/path?other_param=value'); | ||
expect(removeOauthParams('http://localhost/path?access_token=value&state=1')).toEqual('http://localhost/path'); | ||
expect(removeOauthParams('http://localhost/path?access_token=value&token_type=value')).toEqual('http://localhost/path'); | ||
expect(removeOauthParams('http://localhost/path?access_token=value&other_param=1&token_type=value')).toEqual('http://localhost/path?other_param=1'); | ||
expect(removeOauthParams('http://localhost/path?access_token=value&other_param=1')).toEqual('http://localhost/path?other_param=1'); | ||
expect(removeOauthParams('http://localhost/path?other_param=1&access_token=value')).toEqual('http://localhost/path?other_param=1'); | ||
expect(removeOauthParams('http://localhost/path?access_token=value&other_param=1&state=2')).toEqual('http://localhost/path?other_param=1'); | ||
expect(removeOauthParams('http://localhost/path?access_token=value&state=2&other_param=1')).toEqual('http://localhost/path?other_param=1'); | ||
}); | ||
}); |
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,47 @@ | ||
import axios from 'axios'; | ||
|
||
const ACCESS_TOKEN_KEY = 'abeAccessToken'; | ||
|
||
// axios.interceptors.request.use((config) => { | ||
// if (accessToken) { | ||
// // eslint-disable-next-line no-param-reassign | ||
// config.headers.common.Authorization = `Bearer ${accessToken}`; | ||
// } | ||
// return config; | ||
// }, Promise.reject); | ||
|
||
export function setAccessToken(accessToken) { | ||
localStorage[ACCESS_TOKEN_KEY] = accessToken; | ||
axios.defaults.headers.common.Authorization = `Bearer ${accessToken}`; | ||
} | ||
|
||
export function clearAccessToken() { | ||
localStorage.removeItem(ACCESS_TOKEN_KEY); | ||
axios.defaults.headers.common.Authorization = null; | ||
} | ||
|
||
export function removeOauthParams(url) { | ||
return url | ||
.replace(/([?&])(access_token|expires_in|state|token_type)=[^&]*/g, '$1') | ||
.replace(/([?&])&+/, '$1') | ||
.replace(/[?&]$/, ''); | ||
} | ||
|
||
export function initializeAccessToken() { | ||
const token = localStorage[ACCESS_TOKEN_KEY]; | ||
const match = document.location.search.match(/[&?]access_token=([^&]+)/); | ||
if (match) { | ||
setAccessToken(decodeURIComponent(match[1])); | ||
window.history.replaceState({}, document.title, removeOauthParams(window.location.href)); | ||
} else if (token) { | ||
setAccessToken(token); | ||
} | ||
} | ||
|
||
export function setAccessTokenFromResponse(response) { | ||
const token = response.headers['access-token']; | ||
if (token) { | ||
setAccessToken(token); | ||
} | ||
return response; | ||
} |
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