Auto-refresh OAuth 2 tokens & retry request on 401 response
This commit is contained in:
1 parent
1cf7975319
commit
3d157007f6
4 files changed
+61
-1
No files matched your search
@@ -540,6 +540,15 @@ module Google
|
||||
request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
|
||||
|
||||
result = request.send(connection)
|
||||
if result.status == 401 && authorization.respond_to?(:refresh_token)
|
||||
begin
|
||||
authorization.fetch_access_token!
|
||||
result = request.send(connection)
|
||||
rescue Signet::AuthorizationError
|
||||
# Ignore since we want the original error
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
require 'jwt'
|
||||
require 'signet/oauth_2/client'
|
||||
require 'delegate'
|
||||
|
||||
module Google
|
||||
class APIClient
|
||||
@@ -117,7 +118,21 @@ module Google
|
||||
authorization.grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
|
||||
authorization.extension_parameters = { :assertion => assertion }
|
||||
authorization.fetch_access_token!(options)
|
||||
return authorization
|
||||
return JWTAuthorization.new(authorization, self, person)
|
||||
end
|
||||
end
|
||||
|
||||
class JWTAuthorization < DelegateClass(Signet::OAuth2::Client)
|
||||
def initialize(authorization, asserter, person = nil)
|
||||
@asserter = asserter
|
||||
@person = person
|
||||
super(authorization)
|
||||
end
|
||||
|
||||
def fetch_access_token!(options={})
|
||||
new_authorization = @asserter.authorize(@person, options)
|
||||
__setobj__(new_authorization)
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user