Class: Shakha::Providers::GitHub

Inherits:
Base
  • Object
show all
Defined in:
lib/shakha/providers/github.rb

Constant Summary collapse

AUTHORIZE_URL =
"https://github.com/login/oauth/authorize"
TOKEN_URL =
"https://github.com/login/oauth/access_token"
USER_API_URL =
"https://api.github.com/user"

Instance Method Summary collapse

Instance Method Details

#authorize_url(state:, code_challenge:, redirect_uri:, nonce: nil) ⇒ Object

GitHub OAuth has no nonce concept; the keyword is accepted for a uniform provider interface and ignored.



19
20
21
22
23
24
25
26
27
28
# File 'lib/shakha/providers/github.rb', line 19

def authorize_url(state:, code_challenge:, redirect_uri:, nonce: nil)
  params = {
    client_id: Shakha.config.github_client_id,
    redirect_uri: redirect_uri,
    scope: scopes.join(" "),
    state: state
  }

  "#{AUTHORIZE_URL}?#{URI.encode_www_form(params)}"
end

#exchange_code(code:, code_verifier:, redirect_uri:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/shakha/providers/github.rb', line 30

def exchange_code(code:, code_verifier:, redirect_uri:)
  response = http_post(TOKEN_URL, {
    code: code,
    client_id: Shakha.config.github_client_id,
    client_secret: Shakha.config.github_client_secret,
    redirect_uri: redirect_uri
  }, accept: :json)

  JSON.parse(response.body)
end

#identity_from_response(token_response, expected_nonce: nil) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shakha/providers/github.rb', line 41

def identity_from_response(token_response, expected_nonce: nil)
  access_token = token_response["access_token"]
  raise OAuthError, "No access_token received" unless access_token

  user_data = fetch_user(access_token)

  {
    provider: :github,
    uid: user_data["id"].to_s,
    email: user_data["email"],
    name: user_data["name"] || user_data["login"],
    picture: user_data["avatar_url"]
  }
end

#provider_nameObject



13
14
15
# File 'lib/shakha/providers/github.rb', line 13

def provider_name
  :github
end

#scopesObject



56
57
58
# File 'lib/shakha/providers/github.rb', line 56

def scopes
  %w[user:email]
end