forked from drod21/redux-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
825 additions
and
570 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 |
---|---|---|
|
@@ -5,7 +5,7 @@ node_js: | |
- "4" | ||
|
||
before_install: | ||
- npm install [email protected] | ||
- npm install [email protected] immutable babel-polyfill | ||
|
||
after_success: | ||
- coveralls |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,47 @@ | ||
import { USER_EXPIRED, REDIRECT_SUCCESS, USER_FOUND } from '../constants' | ||
import { USER_EXPIRED, REDIRECT_SUCCESS, USER_FOUND, SILENT_RENEW_ERROR, USER_EXPIRING, SESSION_TERMINATED } from '../constants' | ||
|
||
// dispatched when the existing user expired | ||
export function userExpired() { | ||
return { | ||
type: USER_EXPIRED | ||
}; | ||
} | ||
|
||
// dispatched after a successful redirect callback | ||
export function redirectSuccess(user) { | ||
return { | ||
type: REDIRECT_SUCCESS, | ||
payload: user | ||
}; | ||
} | ||
|
||
// dispatched when a user has been found in storage | ||
export function userFound(user) { | ||
return { | ||
type: USER_FOUND, | ||
payload: user | ||
}; | ||
} | ||
|
||
// dispatched when silent renew fails | ||
// payload: the error | ||
export function silentRenewError(error) { | ||
return { | ||
type: SILENT_RENEW_ERROR, | ||
payload: error | ||
}; | ||
} | ||
|
||
// dispatched when the user is logged out | ||
export function sessionTerminated() { | ||
return { | ||
type: SESSION_TERMINATED | ||
}; | ||
} | ||
|
||
// dispatched when the user is expiring (just before a silent renew is triggered) | ||
export function userExpiring() { | ||
return { | ||
type: USER_EXPIRING | ||
}; | ||
} |
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,7 @@ | ||
export const STORAGE_KEY = 'oidc.expired'; | ||
export const USER_EXPIRED = 'redux-oidc/USER_EXPIRED'; | ||
export const REDIRECT_SUCCESS = 'redux-oidc/REDIRECT_SUCCESS'; | ||
export const USER_LOADED = 'redux-oidc/USER_LOADED'; | ||
export const SILENT_RENEW_ERROR = 'redux-oidc/SILENT_RENEW_ERROR'; | ||
export const SESSION_TERMINATED = 'redux-oidc/SESSION_TERMINATED'; | ||
export const USER_EXPIRING = 'redux-oidc/USER_EXPIRING'; |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
import createUserManager from './createUserManager'; | ||
|
||
export default { createUserManager }; | ||
export const createUserManager = require('./createUserManager').default; | ||
export const processSilentRenew = require('./processSilentRenew').default; |
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,6 @@ | ||
import createUserManager from './createUserManager'; | ||
|
||
export default function processSilentRenew() { | ||
const mgr = createUserManager(); | ||
mgr.signinSilentCallback(); | ||
} |
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,75 @@ | ||
import { STORAGE_KEY } from './constants'; | ||
import { userExpired, userFound, silentRenewError, sessionTerminated } from './actions'; | ||
|
||
// store the user here to prevent future promise calls to getUser() | ||
export let storedUser = null; | ||
|
||
// helper function to set the stored user manually (for testing) | ||
export function setStoredUser(user) { | ||
storedUser = user; | ||
} | ||
|
||
// helper function to remove the stored user manually (for testing) | ||
export function removeStoredUser() { | ||
storedUser = null; | ||
} | ||
|
||
// callback for the user manager's getUser().then() | ||
export function getUserSuccessCallback(next, userManager, user, triggerAuthFlow, action) { | ||
if (!user || user.expired) { | ||
// IF: user is expired | ||
next(userExpired()); | ||
if (triggerAuthFlow) { | ||
// IF: auth flow should be triggered | ||
userManager.signinRedirect({ data: { | ||
redirectUrl: window.location.href | ||
} | ||
}); | ||
} else { | ||
return next(action); | ||
} | ||
} else { | ||
// ELSE: user is NOT expired | ||
localStorage.removeItem(STORAGE_KEY); | ||
storedUser = user; | ||
next(userFound(user)); | ||
return next(action); | ||
} | ||
} | ||
|
||
export function getUserErrorCallback(error) { | ||
localStorage.removeItem(STORAGE_KEY); | ||
throw new Error(`Error loading user: ${error.message}`); | ||
} | ||
|
||
// the middleware creator function | ||
export default function createOidcMiddleware(userManager, shouldValidate, triggerAuthFlow = true) { | ||
if (!userManager) { | ||
throw new Error('You must provide a user manager!'); | ||
} | ||
|
||
if (!shouldValidate || typeof(shouldValidate) !== 'function') { | ||
// set the default shouldValidate() | ||
shouldValidate = (state, action) => true; | ||
} | ||
|
||
// the middleware | ||
return (store) => (next) => (action) => { | ||
if (shouldValidate(store.getState(), action) && !localStorage.getItem(STORAGE_KEY)) { | ||
// IF: validation should occur... | ||
if (!storedUser || storedUser.expired) { | ||
// IF: user hasn't been found or is expired... | ||
localStorage.setItem(STORAGE_KEY, true); | ||
userManager.getUser() | ||
.then((user) => getUserSuccessCallback(next, userManager, user, triggerAuthFlow, action)) | ||
.catch(getUserErrorCallback); | ||
} else { | ||
// ELSE: user has been found and NOT is expired... | ||
return next(action); | ||
} | ||
} else { | ||
// ELSE: validation should NOT occur... | ||
return next(action); | ||
} | ||
} | ||
}; |
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.