react-oauth2-pkce
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -10,2 +10,4 @@ import { PKCECodePair } from './pkce'; | ||
scopes: string[]; | ||
autoRefresh?: boolean; | ||
refreshSlack?: number; | ||
} | ||
@@ -25,4 +27,14 @@ export interface AuthTokens { | ||
} | ||
export declare class AuthService { | ||
export interface TokenRequestBody { | ||
clientId: string; | ||
grantType: string; | ||
redirectUri?: string; | ||
refresh_token?: string; | ||
clientSecret?: string; | ||
code?: string; | ||
codeVerifier?: string; | ||
} | ||
export declare class AuthService<TIDToken = JWTIDToken> { | ||
props: AuthServiceProps; | ||
timeout?: number; | ||
constructor(props: AuthServiceProps); | ||
@@ -42,4 +54,6 @@ getUser(): {}; | ||
authorize(): boolean; | ||
fetchToken(code: string): Promise<AuthTokens>; | ||
fetchToken(code: string, isRefresh?: boolean): Promise<AuthTokens>; | ||
armRefreshTimer(refreshToken: string, timeoutDuration: number): void; | ||
startTimer(): void; | ||
restoreUri(): void; | ||
} |
@@ -141,2 +141,4 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
}); | ||
} else if (this.props.autoRefresh) { | ||
this.startTimer(); | ||
} | ||
@@ -283,3 +285,7 @@ } | ||
_proto.fetchToken = function fetchToken(code) { | ||
_proto.fetchToken = function fetchToken(code, isRefresh) { | ||
if (isRefresh === void 0) { | ||
isRefresh = false; | ||
} | ||
try { | ||
@@ -293,9 +299,7 @@ var _this7 = this; | ||
provider = _this7$props.provider, | ||
redirectUri = _this7$props.redirectUri; | ||
redirectUri = _this7$props.redirectUri, | ||
_this7$props$autoRefr = _this7$props.autoRefresh, | ||
autoRefresh = _this7$props$autoRefr === void 0 ? false : _this7$props$autoRefr; | ||
var grantType = 'authorization_code'; | ||
var pkce = _this7.getPkce(); | ||
var codeVerifier = pkce.codeVerifier; | ||
var payload = _extends(_extends({ | ||
@@ -306,8 +310,21 @@ clientId: clientId | ||
} : {}), {}, { | ||
code: code, | ||
redirectUri: redirectUri, | ||
grantType: grantType, | ||
codeVerifier: codeVerifier | ||
grantType: grantType | ||
}); | ||
if (isRefresh) { | ||
payload = _extends(_extends({}, payload), {}, { | ||
grantType: 'refresh_token', | ||
refresh_token: code | ||
}); | ||
} else { | ||
var pkce = _this7.getPkce(); | ||
var codeVerifier = pkce.codeVerifier; | ||
payload = _extends(_extends({}, payload), {}, { | ||
code: code, | ||
codeVerifier: codeVerifier | ||
}); | ||
} | ||
return Promise.resolve(fetch(provider + "/token", { | ||
@@ -325,2 +342,6 @@ headers: { | ||
if (autoRefresh) { | ||
_this7.startTimer(); | ||
} | ||
return json; | ||
@@ -334,2 +355,67 @@ }); | ||
_proto.armRefreshTimer = function armRefreshTimer(refreshToken, timeoutDuration) { | ||
var _this8 = this; | ||
var _this$props$refreshSl = this.props.refreshSlack, | ||
refreshSlack = _this$props$refreshSl === void 0 ? 10 : _this$props$refreshSl; | ||
if (this.timeout) { | ||
clearTimeout(this.timeout); | ||
} | ||
this.timeout = window.setTimeout(function () { | ||
_this8.fetchToken(refreshToken, true).then(function (_ref2) { | ||
var newRefreshToken = _ref2.refresh_token, | ||
expiresIn = _ref2.expires_in; | ||
var now = new Date().getTime(); | ||
var expiresAt = now + (expiresIn - refreshSlack) * 1000; | ||
var timeout = expiresAt - now; | ||
if (timeout > 0) { | ||
_this8.armRefreshTimer(newRefreshToken, timeout); | ||
} else { | ||
_this8.removeItem('auth'); | ||
_this8.removeCodeFromLocation(); | ||
} | ||
})["catch"](function (e) { | ||
_this8.removeItem('auth'); | ||
_this8.removeCodeFromLocation(); | ||
console.warn({ | ||
e: e | ||
}); | ||
}); | ||
}, timeoutDuration); | ||
}; | ||
_proto.startTimer = function startTimer() { | ||
var _this$props$refreshSl2 = this.props.refreshSlack, | ||
refreshSlack = _this$props$refreshSl2 === void 0 ? 10 : _this$props$refreshSl2; | ||
var authTokens = this.getAuthTokens(); | ||
if (!authTokens) { | ||
return; | ||
} | ||
var refreshToken = authTokens.refresh_token, | ||
expiresIn = authTokens.expires_in; | ||
if (!expiresIn || !refreshToken) { | ||
return; | ||
} | ||
var now = new Date().getTime(); | ||
var expiresAt = now + (expiresIn - refreshSlack) * 1000; | ||
var timeout = expiresAt - now; | ||
if (timeout > 0) { | ||
this.armRefreshTimer(refreshToken, timeout); | ||
} else { | ||
this.removeItem('auth'); | ||
this.removeCodeFromLocation(); | ||
} | ||
}; | ||
_proto.restoreUri = function restoreUri() { | ||
@@ -336,0 +422,0 @@ var uri = window.localStorage.getItem('preAuthUri'); |
@@ -138,2 +138,4 @@ import React, { useContext } from 'react'; | ||
}); | ||
} else if (this.props.autoRefresh) { | ||
this.startTimer(); | ||
} | ||
@@ -280,3 +282,7 @@ } | ||
_proto.fetchToken = function fetchToken(code) { | ||
_proto.fetchToken = function fetchToken(code, isRefresh) { | ||
if (isRefresh === void 0) { | ||
isRefresh = false; | ||
} | ||
try { | ||
@@ -290,9 +296,7 @@ var _this7 = this; | ||
provider = _this7$props.provider, | ||
redirectUri = _this7$props.redirectUri; | ||
redirectUri = _this7$props.redirectUri, | ||
_this7$props$autoRefr = _this7$props.autoRefresh, | ||
autoRefresh = _this7$props$autoRefr === void 0 ? false : _this7$props$autoRefr; | ||
var grantType = 'authorization_code'; | ||
var pkce = _this7.getPkce(); | ||
var codeVerifier = pkce.codeVerifier; | ||
var payload = _extends(_extends({ | ||
@@ -303,8 +307,21 @@ clientId: clientId | ||
} : {}), {}, { | ||
code: code, | ||
redirectUri: redirectUri, | ||
grantType: grantType, | ||
codeVerifier: codeVerifier | ||
grantType: grantType | ||
}); | ||
if (isRefresh) { | ||
payload = _extends(_extends({}, payload), {}, { | ||
grantType: 'refresh_token', | ||
refresh_token: code | ||
}); | ||
} else { | ||
var pkce = _this7.getPkce(); | ||
var codeVerifier = pkce.codeVerifier; | ||
payload = _extends(_extends({}, payload), {}, { | ||
code: code, | ||
codeVerifier: codeVerifier | ||
}); | ||
} | ||
return Promise.resolve(fetch(provider + "/token", { | ||
@@ -322,2 +339,6 @@ headers: { | ||
if (autoRefresh) { | ||
_this7.startTimer(); | ||
} | ||
return json; | ||
@@ -331,2 +352,67 @@ }); | ||
_proto.armRefreshTimer = function armRefreshTimer(refreshToken, timeoutDuration) { | ||
var _this8 = this; | ||
var _this$props$refreshSl = this.props.refreshSlack, | ||
refreshSlack = _this$props$refreshSl === void 0 ? 10 : _this$props$refreshSl; | ||
if (this.timeout) { | ||
clearTimeout(this.timeout); | ||
} | ||
this.timeout = window.setTimeout(function () { | ||
_this8.fetchToken(refreshToken, true).then(function (_ref2) { | ||
var newRefreshToken = _ref2.refresh_token, | ||
expiresIn = _ref2.expires_in; | ||
var now = new Date().getTime(); | ||
var expiresAt = now + (expiresIn - refreshSlack) * 1000; | ||
var timeout = expiresAt - now; | ||
if (timeout > 0) { | ||
_this8.armRefreshTimer(newRefreshToken, timeout); | ||
} else { | ||
_this8.removeItem('auth'); | ||
_this8.removeCodeFromLocation(); | ||
} | ||
})["catch"](function (e) { | ||
_this8.removeItem('auth'); | ||
_this8.removeCodeFromLocation(); | ||
console.warn({ | ||
e: e | ||
}); | ||
}); | ||
}, timeoutDuration); | ||
}; | ||
_proto.startTimer = function startTimer() { | ||
var _this$props$refreshSl2 = this.props.refreshSlack, | ||
refreshSlack = _this$props$refreshSl2 === void 0 ? 10 : _this$props$refreshSl2; | ||
var authTokens = this.getAuthTokens(); | ||
if (!authTokens) { | ||
return; | ||
} | ||
var refreshToken = authTokens.refresh_token, | ||
expiresIn = authTokens.expires_in; | ||
if (!expiresIn || !refreshToken) { | ||
return; | ||
} | ||
var now = new Date().getTime(); | ||
var expiresAt = now + (expiresIn - refreshSlack) * 1000; | ||
var timeout = expiresAt - now; | ||
if (timeout > 0) { | ||
this.armRefreshTimer(refreshToken, timeout); | ||
} else { | ||
this.removeItem('auth'); | ||
this.removeCodeFromLocation(); | ||
} | ||
}; | ||
_proto.restoreUri = function restoreUri() { | ||
@@ -333,0 +419,0 @@ var uri = window.localStorage.getItem('preAuthUri'); |
{ | ||
"name": "react-oauth2-pkce", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Authenticate against generic OAuth2 using PKCE", | ||
@@ -5,0 +5,0 @@ "author": "Gardner Bickford <gardner@bickford.nz>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
74460
891