Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tokens rotation with additional headers - helper functions #854

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Sources/AppAuthCore/OIDAuthState.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,48 @@ static NSString *const kRefreshTokenRequestException =
(nullable NSDictionary<NSString *, NSString *> *)additionalParameters
dispatchQueue:(dispatch_queue_t)dispatchQueue;

/*! @brief Calls the block with a valid access token (refreshing it first, if needed), or if a
refresh was needed and failed, with the error that caused it to fail.
@param action The block to execute with a fresh token. This block will be executed on the main
thread.
@param additionalHeaders Additional parameters for the token request if token is
refreshed.
*/
- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshHeaders:
(nullable NSDictionary<NSString *, NSString *> *)additionalHeaders;

/*! @brief Calls the block with a valid access token (refreshing it first, if needed), or if a
refresh was needed and failed, with the error that caused it to fail.
@param action The block to execute with a fresh token. This block will be executed on the main
thread.
@param additionalHeaders Additional parameters for the token request if token is
refreshed.
@param dispatchQueue The dispatchQueue on which to dispatch the action block.
*/
- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshHeaders:
(nullable NSDictionary<NSString *, NSString *> *)additionalHeaders
dispatchQueue:(dispatch_queue_t)dispatchQueue;

/*! @brief Calls the block with a valid access token (refreshing it first, if needed), or if a
refresh was needed and failed, with the error that caused it to fail.
@param action The block to execute with a fresh token. This block will be executed on the main
thread.
@param additionalParameters Additional parameters for the token request if token is
refreshed.
@param additionalHeaders Additional parameters for the token request if token is
refreshed.
@param dispatchQueue The dispatchQueue on which to dispatch the action block.
*/
- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshParameters:
(nullable NSDictionary<NSString *, NSString *> *)additionalParameters
additionalRefreshHeaders:
(nullable NSDictionary<NSString *, NSString *> *)additionalHeaders
dispatchQueue:(dispatch_queue_t)dispatchQueue;


/*! @brief Forces a token refresh the next time @c OIDAuthState.performActionWithFreshTokens: is
called, even if the current tokens are considered valid.
*/
Expand Down
33 changes: 32 additions & 1 deletion Sources/AppAuthCore/OIDAuthState.m
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,36 @@ - (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshParameters:
(nullable NSDictionary<NSString *, NSString *> *)additionalParameters
dispatchQueue:(dispatch_queue_t)dispatchQueue {
[self performActionWithFreshTokens:action
additionalRefreshParameters:additionalParameters
additionalRefreshHeaders:nil
dispatchQueue:dispatch_get_main_queue()];
}

- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshHeaders:
(nullable NSDictionary<NSString *, NSString *> *)additionalHeaders {
[self performActionWithFreshTokens:action
additionalRefreshHeaders:additionalHeaders
dispatchQueue:dispatch_get_main_queue()];
}

- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshHeaders:
(nullable NSDictionary<NSString *, NSString *> *)additionalHeaders
dispatchQueue:(dispatch_queue_t)dispatchQueue {
[self performActionWithFreshTokens:action
additionalRefreshParameters:nil
additionalRefreshHeaders:additionalHeaders
dispatchQueue:dispatch_get_main_queue()];
}

- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshParameters:
(nullable NSDictionary<NSString *, NSString *> *)additionalParameters
additionalRefreshHeaders:
(nullable NSDictionary<NSString *, NSString *> *)additionalHeaders
dispatchQueue:(dispatch_queue_t)dispatchQueue {

if ([self isTokenFresh]) {
// access token is valid within tolerance levels, perform action
Expand Down Expand Up @@ -544,7 +574,8 @@ - (void)performActionWithFreshTokens:(OIDAuthStateAction)action

// refresh the tokens
OIDTokenRequest *tokenRefreshRequest =
[self tokenRefreshRequestWithAdditionalParameters:additionalParameters];
[self tokenRefreshRequestWithAdditionalParameters:additionalParameters
additionalHeaders:additionalHeaders];
[OIDAuthorizationService performTokenRequest:tokenRefreshRequest
originalAuthorizationResponse:_lastAuthorizationResponse
callback:^(OIDTokenResponse *_Nullable response,
Expand Down