1.17.8 (latest) 1.17.7 1.16.2 1.15.1 1.14.0 1.13.1 1.12.2 Reference documentation and code samples for the googleauth class Google::Auth::WebUserAuthorizer.

Varation on [UserAuthorizer](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-UserAuthorizer.md.txt "Google::Auth::UserAuthorizer (class)") adapted for Rack based
web applications.


Example usage:

    get('/') do
      user_id = request.session['user_email']
      credentials = authorizer.get_credentials(user_id, request)
      if credentials.nil?
        redirect authorizer.get_authorization_url(user_id: user_id,
                                                  request: request)
      end
      # Credentials are valid, can call APIs
      ...

end

get('/oauth2callback') do
url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(
request)
redirect url
end

<br />

Instead of implementing the callback directly, applications are
encouraged to use [CallbackApp](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-WebUserAuthorizer-CallbackApp.md.txt "Google::Auth::WebUserAuthorizer::CallbackApp (class)") instead.

## Inherits

- [Google::Auth::UserAuthorizer](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-UserAuthorizer.md.txt)

## Methods

### .default

```ruby
def self.default()
```
Returns the value of attribute default.

### .default=

```ruby
def self.default=(value)
```
Sets the attribute default

### .extract_callback_state

```ruby
def self.extract_callback_state(request) -> Array<Hash, String>
```
Extract the callback state from the request
**Parameter**

- **request** (Rack::Request) --- Current request
**Returns**

- (Array\<Hash, String\>) --- Callback state and redirect URI

### .handle_auth_callback_deferred

```ruby
def self.handle_auth_callback_deferred(request) -> String, nil
```
Handle the result of the oauth callback. This version defers the
exchange of the code by temporarily stashing the results in the user's
session. This allows apps to use the generic
[CallbackApp](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-WebUserAuthorizer-CallbackApp.md.txt "Google::Auth::WebUserAuthorizer::CallbackApp (class)") handler for the callback
without any additional customization.


<br />

Apps that wish to handle the callback directly should use
[#handle_auth_callback](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-WebUserAuthorizer.md.txt#Google__Auth__WebUserAuthorizer_handle_auth_callback_instance_ "Google::Auth::WebUserAuthorizer#handle_auth_callback (method)") instead.
**Parameter**

- **request** (Rack::Request) --- Current request
**Returns**

- (String, nil) --- Redirect URI if successfully extracted, nil otherwise

### .principal

```ruby
def self.principal() -> Symbol
```
Returns the principal identifier for this web authorizer
This is a class method that returns a symbol since
we might not have a client_id in the static callback context
**Returns**

- (Symbol) --- The symbol for web user authorization

### .validate_callback_state

```ruby
def self.validate_callback_state(state, request)
```
Verifies the results of an authorization callback
**Parameters**

- **state** (Hash) --- Callback state
- **request** (Rack::Request) --- Current request
**Raises**

- ([Google::Auth::AuthorizationError](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-AuthorizationError.md.txt)) --- If the authorization code is missing, there's an error in the callback state, or the state token doesn't match

### #get_authorization_url

```ruby
def get_authorization_url(options = {}) -> String
```
Build the URL for requesting authorization.
**Parameters**

- **login_hint** (String) --- Login hint if need to authorize a specific account. Should be a user's email address or unique profile ID.
- **request** (Rack::Request) --- Current request
- **redirect_to** (String) --- Optional URL to proceed to after authorization complete. Defaults to the current URL.
- **scope** (String, Array\<String\>) --- Authorization scope to request. Overrides the instance scopes if not nil.
- **state** (Hash) --- Optional key-values to be returned to the oauth callback.
**Returns**

- (String) --- Authorization url
**Raises**

- ([Google::Auth::InitializationError](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-InitializationError.md.txt)) --- If request is nil or request.session is nil

### #get_credentials

```ruby
def get_credentials(user_id, request = nil, scope = nil) -> Google::Auth::UserRefreshCredentials
```
Fetch stored credentials for the user from the given request session.
**Parameters**

- **user_id** (String) --- Unique ID of the user for loading/storing credentials.
- **request** (Rack::Request) --- Current request. Optional. If omitted, this will attempt to fall back on the base class behavior of reading from the token store.
- **scope** (Array\<String\>, String) --- If specified, only returns credentials that have all the \\ requested scopes
**Returns**

- ([Google::Auth::UserRefreshCredentials](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-UserRefreshCredentials.md.txt)) --- Stored credentials, nil if none present
**Raises**

- ([Google::Auth::AuthorizationError](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-AuthorizationError.md.txt)) --- If the authorization code is missing, there's an error in the request, or the state token doesn't match

### #handle_auth_callback

```ruby
def handle_auth_callback(user_id, request) -> Google::Auth::UserRefreshCredentials, String
```
Handle the result of the oauth callback. Exchanges the authorization
code from the request and persists to storage.
**Parameters**

- **user_id** (String) --- Unique ID of the user for loading/storing credentials.
- **request** (Rack::Request) --- Current request
**Returns**

- ([Google::Auth::UserRefreshCredentials](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-UserRefreshCredentials.md.txt), String) --- credentials \& next URL to redirect to

### #initialize

```ruby
def initialize(client_id, scope, token_store, legacy_callback_uri = nil, callback_uri: nil, code_verifier: nil) -> WebUserAuthorizer
```
Initialize the authorizer
**Parameters**

- **client_id** (Google::Auth::ClientID) --- Configured ID \& secret for this application
- **scope** (String, Array\<String\>) --- Authorization scope to request
- **token_store** (Google::Auth::Stores::TokenStore) --- Backing storage for persisting user credentials
- **legacy_callback_uri** (String) --- URL (either absolute or relative) of the auth callback. Defaults to '/oauth2callback'. @deprecated This field is deprecated. Instead, use the keyword argument callback_uri.
- **code_verifier** (String) *(defaults to: nil)* --- Random string of 43-128 chars used to verify the key exchange using PKCE.
**Returns**

- ([WebUserAuthorizer](https://docs.cloud.google.com/ruby/docs/reference/googleauth/latest/Google-Auth-WebUserAuthorizer.md.txt)) --- a new instance of WebUserAuthorizer

## Constants

### STATE_PARAM

**value:**"state".freeze

### AUTH_CODE_KEY

**value:**"code".freeze

### ERROR_CODE_KEY

**value:**"error".freeze

### SESSION_ID_KEY

**value:**"session_id".freeze

### CALLBACK_STATE_KEY

**value:**"g-auth-callback".freeze

### CURRENT_URI_KEY

**value:**"current_uri".freeze

### XSRF_KEY

**value:**"g-xsrf-token".freeze

### SCOPE_KEY

**value:**"scope".freeze

### NIL_REQUEST_ERROR

**value:**"Request is required.".freeze

### NIL_SESSION_ERROR

**value:**"Sessions must be enabled".freeze

### MISSING_AUTH_CODE_ERROR

**value:**"Missing authorization code in request".freeze

### AUTHORIZATION_ERROR

**value:**"Authorization error: %s".freeze

### INVALID_STATE_TOKEN_ERROR

**value:**"State token does not match expected value".freeze