Class: OmniauthOidc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/omniauth/oidc/client.rb

Overview

Custom OIDC client using Net::HTTP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
# File 'lib/omniauth/oidc/client.rb', line 11

def initialize(options = {})
  @identifier = options[:identifier] || options["identifier"]
  @secret = options[:secret] || options["secret"]
  @authorization_endpoint = options[:authorization_endpoint] || options["authorization_endpoint"]
  @token_endpoint = options[:token_endpoint] || options["token_endpoint"]
  @userinfo_endpoint = options[:userinfo_endpoint] || options["userinfo_endpoint"]
  @redirect_uri = options[:redirect_uri] || options["redirect_uri"]
end

Instance Attribute Details

#authorization_endpointObject

Returns the value of attribute authorization_endpoint.



8
9
10
# File 'lib/omniauth/oidc/client.rb', line 8

def authorization_endpoint
  @authorization_endpoint
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/omniauth/oidc/client.rb', line 8

def host
  @host
end

#identifierObject

Returns the value of attribute identifier.



8
9
10
# File 'lib/omniauth/oidc/client.rb', line 8

def identifier
  @identifier
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



8
9
10
# File 'lib/omniauth/oidc/client.rb', line 8

def redirect_uri
  @redirect_uri
end

#secretObject

Returns the value of attribute secret.



8
9
10
# File 'lib/omniauth/oidc/client.rb', line 8

def secret
  @secret
end

#token_endpointObject

Returns the value of attribute token_endpoint.



8
9
10
# File 'lib/omniauth/oidc/client.rb', line 8

def token_endpoint
  @token_endpoint
end

#userinfo_endpointObject

Returns the value of attribute userinfo_endpoint.



8
9
10
# File 'lib/omniauth/oidc/client.rb', line 8

def userinfo_endpoint
  @userinfo_endpoint
end

Instance Method Details

#access_token!(params = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/omniauth/oidc/client.rb', line 42

def access_token!(params = {}) # rubocop:disable Metrics/MethodLength
  body_params = {
    grant_type: "authorization_code",
    code: params[:code],
    redirect_uri: params[:redirect_uri] || @redirect_uri,
    client_id: identifier,
    client_secret: secret
  }

  # Add PKCE verifier if provided
  body_params[:code_verifier] = params[:code_verifier] if params[:code_verifier]

  OmniauthOidc::Logging.instrument("token.exchange", code: "[FILTERED]") do
    response = HttpClient.post(
      token_endpoint,
      body: URI.encode_www_form(body_params),
      headers: {
        "Content-Type" => "application/x-www-form-urlencoded",
        "Accept" => "application/json"
      }
    )

    ResponseObjects::AccessToken.new(response)
  end
rescue HttpClient::HttpError => e
  raise OmniauthOidc::TokenError, "Token exchange failed: #{e.message}"
end

#authorization_uri(params = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/omniauth/oidc/client.rb', line 20

def authorization_uri(params = {})
  uri = URI.parse(authorization_endpoint)
  query_params = {
    client_id: identifier,
    response_type: params[:response_type] || "code",
    scope: params[:scope] || "openid profile email",
    redirect_uri: params[:redirect_uri] || @redirect_uri,
    state: params[:state],
    nonce: params[:nonce]
  }.compact

  # Add PKCE parameters if provided
  query_params[:code_challenge] = params[:code_challenge] if params[:code_challenge]
  query_params[:code_challenge_method] = params[:code_challenge_method] if params[:code_challenge_method]

  # Add any additional params
  query_params.merge!(params[:extra_params]) if params[:extra_params]

  uri.query = URI.encode_www_form(query_params)
  uri.to_s
end

#userinfo!(access_token) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/omniauth/oidc/client.rb', line 70

def userinfo!(access_token)
  OmniauthOidc::Logging.instrument("userinfo.fetch", endpoint: userinfo_endpoint) do
    response = HttpClient.get(
      userinfo_endpoint,
      headers: {
        "Authorization" => "Bearer #{access_token}",
        "Accept" => "application/json"
      }
    )

    ResponseObjects::UserInfo.new(response)
  end
rescue HttpClient::HttpError => e
  raise OmniauthOidc::TokenError, "Failed to fetch user info: #{e.message}"
end