Class: Spree::Authentication::Strategies::BaseStrategy

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/authentication/strategies/base_strategy.rb

Direct Known Subclasses

EmailPasswordStrategy, OidcStrategy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, request_env:, user_class: nil) ⇒ BaseStrategy

Returns a new instance of BaseStrategy.



30
31
32
33
34
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 30

def initialize(params:, request_env:, user_class: nil)
  @params = params
  @request_env = request_env
  @user_class = user_class || Spree.customer_class
end

Instance Attribute Details

#params ⇒ Object (readonly)

Returns the value of attribute params.



5
6
7
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 5

def params
  @params
end

#request_env ⇒ Object (readonly)

Returns the value of attribute request_env.



5
6
7
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 5

def request_env
  @request_env
end

#user_class ⇒ Object (readonly)

Returns the value of attribute user_class.



5
6
7
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 5

def user_class
  @user_class
end

Class Method Details

.kind ⇒ Symbol

How a client initiates login with this strategy.

:password — the client posts credentials to the auth endpoint. :redirect — the client sends the browser to the identity provider, which returns through the OAuth callback.

Defaults to :password so strategies written before provider discovery existed keep describing themselves correctly.

Returns:

  • (Symbol)


17
18
19
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 17

def kind
  :password
end

.label ⇒ String?

Human-readable provider name for the login button. Only meaningful for :redirect strategies; defaults to nil so password strategies render their standard form instead of a button.

Returns:

  • (String, nil)


25
26
27
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 25

def label
  nil
end

Instance Method Details

#authenticate ⇒ Spree::ServiceModule::Result

Returns Result object with user on success

Returns:

Raises:

  • (NotImplementedError)


56
57
58
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 56

def authenticate
  raise NotImplementedError, 'Subclass must implement #authenticate'
end

#authorization_url(state:) ⇒ String

Where to send the browser to begin authentication. Redirect strategies must implement this; password strategies never call it.

Parameters:

  • state (String) —

    opaque CSRF token echoed back to the callback

Returns:

  • (String) —

    the identity provider's authorization URL

Raises:

  • (NotImplementedError)


41
42
43
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 41

def authorization_url(state:)
  raise NotImplementedError, 'Redirect strategies must implement #authorization_url'
end

#callback ⇒ Spree::ServiceModule::Result

Completes a redirect login from the identity provider's callback params. Redirect strategies must implement this; password strategies use #authenticate instead.

Returns:

Raises:

  • (NotImplementedError)


50
51
52
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 50

def callback
  raise NotImplementedError, 'Redirect strategies must implement #callback'
end

#provider ⇒ String

Returns provider identifier (e.g., 'google', 'email')

Returns:

  • (String)

Raises:

  • (NotImplementedError)


62
63
64
# File 'app/models/spree/authentication/strategies/base_strategy.rb', line 62

def provider
  raise NotImplementedError, 'Subclass must implement #provider'
end