module FidorApi::Client::Authentication

Attributes

token[RW]

Public Instance Methods

authorize_complete(redirect_uri:, code:) click to toggle source

oAuth2 - Authorization Code Grant Flow - Complete

# File lib/fidor_api/client/authentication.rb, line 36
def authorize_complete(redirect_uri:, code:)
  self.token = oauth_token(
    grant_type:   'authorization_code',
    client_id:    config.client_id,
    redirect_uri: redirect_uri,
    code:         code
  )
end
authorize_start(redirect_uri:, state: SecureRandom.hex(8)) click to toggle source

oAuth2 - Authorization Code Grant Flow - Start

# File lib/fidor_api/client/authentication.rb, line 25
def authorize_start(redirect_uri:, state: SecureRandom.hex(8))
  check_flow_support! :authorization_code

  "#{config.environment.auth_redirect_host}/oauth/authorize" \
    + "?client_id=#{config.client_id}" \
    + "&redirect_uri=#{CGI.escape(redirect_uri)}" \
    + "&state=#{state}" \
    + '&response_type=code'
end
client_login() click to toggle source

oAuth2 - Client Credentials Flow

# File lib/fidor_api/client/authentication.rb, line 18
def client_login
  check_flow_support! :client_credentials

  self.token = oauth_token(grant_type: 'client_credentials')
end
login(username:, password:, grant_type: 'password') click to toggle source

oAuth2 - Resource Owner Password Grant Flow

# File lib/fidor_api/client/authentication.rb, line 7
def login(username:, password:, grant_type: 'password')
  check_flow_support! :resource_owner_password_credentials

  self.token = oauth_token(
    grant_type: grant_type,
    username:   username,
    password:   password
  )
end

Private Instance Methods

check_flow_support!(symbol) click to toggle source
# File lib/fidor_api/client/authentication.rb, line 47
def check_flow_support!(symbol)
  raise Errors::NotSupported \
    unless config.environment.auth_methods.include? symbol
end
oauth_token(**body) click to toggle source
# File lib/fidor_api/client/authentication.rb, line 52
def oauth_token(**body)
  response = connection(host: config.environment.auth_host).post(
    '/oauth/token',
    body: body,
    auth: [config.client_id, config.client_secret]
  )

  Token.new(response.body)
end