Class: Grape::OAuth2::Configuration

Inherits:
Object
  • Object
show all
Includes:
ClassAccessors, Validation
Defined in:
lib/grape_oauth2/configuration.rb,
lib/grape_oauth2/configuration/validation.rb

Overview

Grape::OAuth2 configuration class. Contains default or customized options that would be used in OAuth2 endpoints and helpers.

Defined Under Namespace

Modules: Validation

Constant Summary collapse

Error =

Default Grape::OAuth2 configuration error class.

Class.new(StandardError)
APIMissing =

Grape::OAuth2 configuration error for missing API required for OAuth2 classes.

Class.new(Error)
DEFAULT_TOKEN_LIFETIME =

Default Access Token TTL (in seconds)

7200
DEFAULT_CODE_LIFETIME =

Default Authorization Code TTL ()in seconds)

1800
DEFAULT_REALM =

Default realm value

'OAuth 2.0'.freeze
SUPPORTED_GRANT_TYPES =

Currently supported (be the gem) OAuth2 grant types

%w(password client_credentials refresh_token).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassAccessors

#access_grant_class, #access_token_class, #client_class, #resource_owner_class, #scopes_validator, #token_generator

Methods included from Validation

#check!

Constructor Details

#initializeConfiguration



73
74
75
# File 'lib/grape_oauth2/configuration.rb', line 73

def initialize
  reset!
end

Instance Attribute Details

#access_grant_class_nameString

The names of the classes that represents OAuth2 roles



30
31
32
# File 'lib/grape_oauth2/configuration.rb', line 30

def access_grant_class_name
  @access_grant_class_name
end

#access_token_class_nameString

The names of the classes that represents OAuth2 roles



30
31
32
# File 'lib/grape_oauth2/configuration.rb', line 30

def access_token_class_name
  @access_token_class_name
end

#access_token_lifetimeObject

Access Token and Authorization Code lifetime in seconds



52
53
54
# File 'lib/grape_oauth2/configuration.rb', line 52

def access_token_lifetime
  @access_token_lifetime
end

#allowed_grant_typesArray<String>

OAuth2 grant types (flows) allowed to be processed



49
50
51
# File 'lib/grape_oauth2/configuration.rb', line 49

def allowed_grant_types
  @allowed_grant_types
end

#authorization_code_lifetimeObject

Access Token and Authorization Code lifetime in seconds



52
53
54
# File 'lib/grape_oauth2/configuration.rb', line 52

def authorization_code_lifetime
  @authorization_code_lifetime
end

#client_class_nameString

The names of the classes that represents OAuth2 roles



30
31
32
# File 'lib/grape_oauth2/configuration.rb', line 30

def client_class_name
  @client_class_name
end

#issue_refresh_tokenBoolean

Specifies whether to generate a Refresh Token when creating an Access Token



58
59
60
# File 'lib/grape_oauth2/configuration.rb', line 58

def issue_refresh_token
  @issue_refresh_token
end

#on_refresh(&block) ⇒ Object

Accessor for on_refresh callback. Set callback proc if called with block or returns current value of the accessor.



71
72
73
# File 'lib/grape_oauth2/configuration.rb', line 71

def on_refresh
  @on_refresh
end

#realmString

Realm value



64
65
66
# File 'lib/grape_oauth2/configuration.rb', line 64

def realm
  @realm
end

#resource_owner_class_nameString

The names of the classes that represents OAuth2 roles



30
31
32
# File 'lib/grape_oauth2/configuration.rb', line 30

def resource_owner_class_name
  @resource_owner_class_name
end

#scopes_validator_class_nameString

Class name for the OAuth2 helper class that validates requested scopes against Access Token scopes



37
38
39
# File 'lib/grape_oauth2/configuration.rb', line 37

def scopes_validator_class_name
  @scopes_validator_class_name
end

#token_authenticator(&block) ⇒ Object

Accessor for Access Token authenticator block. Set it to proc if called with block or returns current value of the accessor.



67
68
69
# File 'lib/grape_oauth2/configuration.rb', line 67

def token_authenticator
  @token_authenticator
end

#token_generator_class_nameString

Class name for the OAuth2 helper class that generates unique token values



43
44
45
# File 'lib/grape_oauth2/configuration.rb', line 43

def token_generator_class_name
  @token_generator_class_name
end

Instance Method Details

#default_token_authenticatorObject

Default Access Token authenticator block. Validates token value passed with the request params.



79
80
81
82
83
# File 'lib/grape_oauth2/configuration.rb', line 79

def default_token_authenticator
  lambda do |request|
    access_token_class.authenticate(request.access_token) || request.invalid_token!
  end
end

#on_refresh_runnable?Boolean

Indicates if on_refresh callback can be invoked.



110
111
112
# File 'lib/grape_oauth2/configuration.rb', line 110

def on_refresh_runnable?
  !on_refresh.nil? && on_refresh != :nothing
end

#reset!Object

Reset configuration to default options values.



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/grape_oauth2/configuration.rb', line 115

def reset!
  initialize_classes
  initialize_authenticators

  self.access_token_lifetime = DEFAULT_TOKEN_LIFETIME
  self.authorization_code_lifetime = DEFAULT_CODE_LIFETIME
  self.allowed_grant_types = %w(password client_credentials)

  self.issue_refresh_token = false
  self.on_refresh = :nothing

  self.realm = DEFAULT_REALM
end