lib/redd/client/oauth2/authorization.rb in redd-0.5.0 vs lib/redd/client/oauth2/authorization.rb in redd-0.6.0

- old
+ new

@@ -36,12 +36,14 @@ # Request an access token from the code that is sent with the redirect. # # @param code [String] The code that was sent in the GET request. # @param set_access [Boolean] Whether to automatically use this token # for all future requests with this client. - # @return [Redd::OAuth2Access] A package of the necessary information - # to access the user's information. + # @return [Redd::OAuth2Access, nil] A package of the necessary + # information to access the user's information or nil if there was + # an error. + # @todo Custom Errors for OAuth2 def request_access(code, set_access = true) response = auth_connection.post "/api/v1/access_token", grant_type: "authorization_code", code: code, redirect_uri: @redirect_uri @@ -73,9 +75,33 @@ when ::String new_access = Redd::OAuth2Access.new(response.body) @access = new_access if set_access new_access end + end + + # Dispose of an access or refresh token when you're done with it. + # + # @param access [Redd::OAuth2Access, String] The token to revoke. + # @param remove_refresh_token [Boolean] Whether you intend to revoke a + # refresh token. + def revoke_access(access, remove_refresh_token = nil) + token = + if remove_refresh_token + extract_attribute(access, :refresh_token) + else + extract_attribute(access, :access_token) + end + + params = {token: token} + + if remove_refresh_token + params[:token_type_hint] = true + elsif remove_refresh_token == false + params[:token_type_hint] = false + end + + auth_connection.post "/api/v1/revoke_token", params end end end end end