Keine Beschreibung

github.rb 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module Oauth
  2. class Github < Oauth::Base
  3. ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
  4. DATA_URL = 'https://api.github.com/user'
  5. EMAIL_URL = 'https://api.github.com/user/emails'
  6. def get_access_token
  7. response = @client.post(ACCESS_TOKEN_URL, @params)
  8. response.body.split('=')[1].split('&')[0]
  9. end
  10. def get_data
  11. response = @client.get(DATA_URL, access_token: @access_token)
  12. @data = JSON.parse(response.body).with_indifferent_access
  13. @data["email"] = get_email if email_is_in_scopes(response.headers)
  14. @uid = @data[:id] ||= @data[:sub]
  15. @data
  16. end
  17. def formatted_user_data
  18. {
  19. provider: 'github',
  20. token: @access_token,
  21. uid: @data['id'],
  22. first_name: @data['login'],
  23. email: @data['email'],
  24. image_url: @data['avatar_url'],
  25. github_profile: @data['html_url']
  26. }
  27. end
  28. def email_is_in_scopes headers
  29. headers.include?("X-OAuth-Scopes") && headers["X-OAuth-Scopes"].split(',').include?("user:email")
  30. end
  31. def get_email
  32. JSON.parse(@client.get(EMAIL_URL, access_token: @access_token).body)[0]["email"]
  33. end
  34. end
  35. end