class Tikkie::Api::V1::Authentication

Provides authentication for the ABN AMRO OAuth API. see developer.abnamro.com/get-started#authentication

Public Class Methods

new(config) click to toggle source
# File lib/tikkie/api/v1/authentication.rb, line 14
def initialize(config)
  @config = config
end

Public Instance Methods

authenticate() click to toggle source
# File lib/tikkie/api/v1/authentication.rb, line 18
def authenticate
  uri = URI.parse(File.join(@config.api_url, "/oauth/token"))

  request = Net::HTTP::Post.new(uri)
  request["Api-Key"] = @config.api_key

  request.set_form_data(
    client_assertion: jwt_token,
    client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
    grant_type: "client_credentials",
    scope: "tikkie"
  )

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.request(request)
  end

  if response.is_a?(Net::HTTPSuccess)
    json = JSON.parse(response.body, symbolize_names: true)

    Tikkie::Api::V1::AccessToken.new(json[:access_token], json[:expires_in])
  else
    raise Tikkie::Api::V1::AuthenticationException, response
  end
end

Private Instance Methods

jwt_token() click to toggle source
# File lib/tikkie/api/v1/authentication.rb, line 46
def jwt_token
  now = Time.now.to_i

  payload = {
    nbf: now - 120,
    exp: now + 120, # Token is valid for 2 minutes
    iss: "Ruby Tikkie client",
    sub: @config.api_key,
    aud: @config.oauth_token_url
  }

  # Send header typ as String, not symbol (JWT version 1.x adds "typ" as String by default).
  headers = {
    "typ" => "JWT"
  }

  JWT.encode(payload, @config.private_data, @config.jwt_hashing_algorithm, headers)
end