Class: Rubino::OAuth::Provider::Github

Inherits:
Rubino::OAuth::Provider show all
Defined in:
lib/rubino/oauth/provider/github.rb

Overview

GitHub OAuth 2.0 provider.

Scopes are sent space-separated (GitHub’s expected delimiter, inherited from #scope_separator). When the authenticated user has set their primary email private, /user returns email: nil; in that case we fall back to /user/emails and pick the primary entry.

Constant Summary collapse

API_BASE =
"https://api.github.com"

Instance Attribute Summary

Attributes inherited from Rubino::OAuth::Provider

#client_id, #client_secret, #metadata, #scopes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rubino::OAuth::Provider

#build_authorize_request, #exchange_code, #id, #initialize, #refresh

Constructor Details

This class inherits a constructor from Rubino::OAuth::Provider

Class Method Details

.authorize_pathObject



19
# File 'lib/rubino/oauth/provider/github.rb', line 19

def self.authorize_path = "/login/oauth/authorize"

.default_scopesObject



21
# File 'lib/rubino/oauth/provider/github.rb', line 21

def self.default_scopes = %w[repo user:email]

.display_nameObject



17
# File 'lib/rubino/oauth/provider/github.rb', line 17

def self.display_name  = "GitHub"

.idObject



16
# File 'lib/rubino/oauth/provider/github.rb', line 16

def self.id            = :github

.siteObject



18
# File 'lib/rubino/oauth/provider/github.rb', line 18

def self.site          = "https://github.com"

.token_pathObject



20
# File 'lib/rubino/oauth/provider/github.rb', line 20

def self.token_path = "/login/oauth/access_token"

Instance Method Details

#fetch_account_info(access_token) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubino/oauth/provider/github.rb', line 43

def (access_token)
  conn = Faraday.new(url: API_BASE) do |f|
    f.headers["Authorization"] = "Bearer #{access_token}"
    f.headers["Accept"] = "application/vnd.github+json"
    f.headers["User-Agent"] = "rubino"
  end

  user = JSON.parse(conn.get("/user").body)
  email = user["email"] || fetch_primary_email(conn)

  {
    account_id: user["id"].to_s,
    account_email: email,
    metadata: { login: user["login"], name: user["name"] }
  }
end

#revoke(access_token) ⇒ Boolean

Revoke an access token by deleting the OAuth grant for our app. docs.github.com/en/rest/apps/oauth-applications#delete-an-app-token Authentication is the app’s (client_id, client_secret) via Basic, not the user token — the token to revoke goes in the JSON body.

Parameters:

  • access_token (String)

    user token to invalidate

Returns:

  • (Boolean)

    true on 204 (success), false otherwise.



32
33
34
35
36
37
38
39
40
41
# File 'lib/rubino/oauth/provider/github.rb', line 32

def revoke(access_token)
  conn = Faraday.new(url: API_BASE) do |f|
    f.request :authorization, :basic, @client_id, @client_secret
    f.headers["Accept"] = "application/vnd.github+json"
    f.headers["Content-Type"] = "application/json"
    f.headers["User-Agent"] = "rubino"
  end
  response = conn.delete("/applications/#{@client_id}/token", JSON.generate(access_token: access_token))
  response.success?
end