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:) ⇒ Object



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

def authorize_url(state:, code_challenge:, redirect_uri:)
  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



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

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) ⇒ Object

Raises:



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

def identity_from_response(token_response)
  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



54
55
56
# File 'lib/shakha/providers/github.rb', line 54

def scopes
  %w[user:email]
end