Class: OAuth2c::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2c/agent.rb

Constant Summary collapse

ConfigError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(authz_url: nil, token_url:, client_id:, client_secret: nil, redirect_uri: nil, client_credentials_on_body: false) ⇒ Agent

Returns a new instance of Agent.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/oauth2c/agent.rb', line 25

def initialize(authz_url: nil, token_url:, client_id:, client_secret: nil,
               redirect_uri: nil, client_credentials_on_body: false)
  @authz_url     = authz_url && authz_url.chomp("/")
  @token_url     = token_url && token_url.chomp("/")
  @client_id     = client_id
  @client_secret = client_secret
  @redirect_uri  = redirect_uri
  @client_credentials_on_body = client_credentials_on_body

  @http_client = HTTP.nodelay
    .accept("application/json")
    .headers(
      "Content-Type": "application/x-www-form-urlencoded; encoding=UTF-8",
      "User-Agent": user_agent_header
    )
  unless @client_credentials_on_body
    @http_client = @http_client.basic_auth(user: @client_id, pass: @client_secret)
  end
end

Instance Method Details

#authz_url(response_type:, state:, scope: [], **params) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/oauth2c/agent.rb', line 54

def authz_url(response_type:, state:, scope: [], **params)
  if @authz_url.nil?
    raise ConfigError, "authz_url not informed for client"
  end

  if @redirect_uri.nil?
    raise ConfigError, "redirect_uri not informed for client"
  end

  params = {
    client_id: @client_id,
    redirect_uri: @redirect_uri,
    response_type: response_type,
    state: state,
    scope: normalize_scope(scope),
    **params
  }

  url = URI.parse(@authz_url)
  url.query = URI.encode_www_form(params.to_a)
  url.to_s
end

#token(grant_type:, scope: [], include_redirect_uri: false, **params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/oauth2c/agent.rb', line 77

def token(grant_type:, scope: [], include_redirect_uri: false, **params)
  params = {
    grant_type: grant_type,
    scope: normalize_scope(scope),
    **params,
  }
  if @client_credentials_on_body
    params.merge!(
      client_id: @client_id,
      client_secret: @client_secret
    )
  end

  if include_redirect_uri
    params[:redirect_uri] = @redirect_uri
  end

  response = @http_client.post(@token_url, body: URI.encode_www_form(params))

  [ response.status.success?, JSON.parse(response.body) ]
end

#user_agent_headerObject



45
46
47
48
49
50
51
52
# File 'lib/oauth2c/agent.rb', line 45

def user_agent_header
  gem_name = "oauth2c"
  gem_version = OAuth2c::VERSION
  app_name = ENV.fetch("APP_NAME", nil) # rubocop:disable Env/OutsideConfig, Env/UndefinedVar
  formatted_app_name = (app_name ? " (#{app_name})" : "")

  "#{gem_name}/#{gem_version}#{formatted_app_name}"
end