Module: Obscured::Doorman::Providers::GitHub

Defined in:
lib/obscured-doorman/providers/github.rb,
lib/obscured-doorman/providers/github/messages.rb,
lib/obscured-doorman/providers/github/strategy.rb,
lib/obscured-doorman/providers/github/access_token.rb,
lib/obscured-doorman/providers/github/configuration.rb

Defined Under Namespace

Classes: AccessToken, Configuration, Strategy

Constant Summary collapse

MESSAGES =
{
  invalid_domain: 'The domain associated with your email address is not whitelisted, please contact system administrator.'
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



20
21
22
# File 'lib/obscured-doorman/providers/github.rb', line 20

def configuration
  @configuration ||= GitHub::Configuration.new
end

Class Method Details

.default_configurationObject



24
25
26
# File 'lib/obscured-doorman/providers/github.rb', line 24

def default_configuration
  configuration.defaults
end

.registered(app) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
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
69
70
71
72
73
74
# File 'lib/obscured-doorman/providers/github.rb', line 29

def self.registered(app)
  app.helpers Doorman::Base::Helpers
  app.helpers Doorman::Helpers

  Warden::Strategies.add(:github, GitHub::Strategy)

  app.get '/doorman/oauth2/github' do
    redirect("#{GitHub.configuration[:authorize_url]}?client_id=#{GitHub.configuration[:client_id]}&response_type=code&scope=#{GitHub.configuration[:scopes]}")
  end

  app.get '/doorman/oauth2/github/callback/?' do
    response = RestClient::Request.new(
      method: :post,
      url: GitHub.configuration[:token_url],
      user: GitHub.configuration[:client_id],
      password: GitHub.configuration[:client_secret],
      payload: "code=#{params[:code]}&grant_type=authorization_code&scope=#{GitHub.configuration[:scopes]}",
      headers: { Accept: 'application/json' }
    ).execute

    json = JSON.parse(response.body)
    token = GitHub::AccessToken.new(
      access_token: json['access_token'],
      token_type: json['token_type'],
      scope: json['scope']
    )

    emails = RestClient.get 'https://api.github.com/user/emails', Authorization: "token #{token.access_token}"
    emails = JSON.parse(emails.body)
    token.emails = emails.map { |e| e['email'] }
    GitHub.configuration[:token] = token

    # Authenticate with :github strategy
    warden.authenticate!(:github)
  rescue RestClient::ExceptionWithResponse => e
    message = JSON.parse(e.response)
    Doorman.logger.error e
    notify :error, "#{message['error_description']} (#{message['error']})"
    redirect(Doorman.configuration.paths[:login])
  ensure
    # Notify if there are any messages from Warden.
    notify :error, warden.message unless warden.message.blank?

    redirect(Doorman.configuration.use_referrer && session[:return_to] ? session.delete(:return_to) : Doorman.configuration.paths[:success])
  end
end

.setup {|configuration| ... } ⇒ Object

Yields:



16
17
18
# File 'lib/obscured-doorman/providers/github.rb', line 16

def setup
  yield(configuration)
end