Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. gem "omniauth-yandex"
  2.  
  3. devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
  4.  
  5. def yandex
  6. require 'net/http'
  7. require 'json' # => false
  8.  
  9. @user = User.from_omniauth(request.env["omniauth.auth"])
  10.  
  11. @client_id = Rails.application.secrets.client_id
  12. @secret = Rails.application.secrets.password
  13. @authorization_code = params[:code]
  14.  
  15. @user.update_attribute(:code, @authorization_code)
  16. @user.update_attribute(:state, params[:state])
  17.  
  18.  
  19. @post_body = "grant_type=authorization_code&code=#{@authorization_code}&client_id=#{@client_id}&client_secret=#{@secret}"
  20.  
  21. @url = "https://oauth.yandex.ru/token"
  22.  
  23. url = URI.parse(@url)
  24. req = Net::HTTP::Post.new(url.request_uri)
  25. req['host'] ="oauth.yandex.ru"
  26. req['Content-Length'] = @post_body.length
  27. req['Content-Type'] = 'application/x-www-form-urlencoded'
  28. req.body = @post_body
  29. http = Net::HTTP.new(url.host, url.port)
  30. http.use_ssl = (url.scheme == "https")
  31.  
  32. @response_mess = http.request(req)
  33.  
  34. refreshhash = JSON.parse(@response_mess.body)
  35. access_token = refreshhash['access_token']
  36. refresh_token = refreshhash['refresh_token']
  37. access_token_expires_at = DateTime.now + refreshhash["expires_in"].to_i.seconds
  38.  
  39.  
  40. if access_token.present? && refresh_token.present? && access_token_expires_at.present?
  41. @user.update_attribute(:access_token, access_token)
  42. @user.update_attribute(:refresh_token, refresh_token)
  43. @user.update_attribute(:expires_in, access_token_expires_at)
  44.  
  45. sign_in(@user)
  46. redirect_to admin_dashboard_index_path
  47. end
  48.  
  49. end
  50.  
  51. require 'rest-client'
  52.  
  53. devise :database_authenticatable, :registerable,
  54. :recoverable, :rememberable, :trackable, :validatable,
  55. :omniauthable, :omniauth_providers => [:yandex]
  56.  
  57. def self.from_omniauth(auth)
  58. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  59. user.provider = auth.provider
  60. user.uid = auth.uid
  61. user.email = auth.info.email
  62. user.code = auth.info.code
  63. user.state = auth.info.state
  64. user.password = Devise.friendly_token[0,20]
  65. end
  66. end
  67.  
  68. def refresh_token_if_expired
  69. if token_expired?
  70. response = RestClient.post "https://oauth.yandex.com/token",
  71. :grant_type => 'refresh_token',
  72. :refresh_token => self.refresh_token
  73.  
  74. refreshhash = JSON.parse(response.body)
  75.  
  76. self.access_token = refreshhash['access_token']
  77. self.expires_in = DateTime.now + refreshhash["expires_in"].to_i.seconds
  78.  
  79. self.save
  80.  
  81. puts 'Saved'
  82. end
  83. end
  84.  
  85. def token_expired?
  86. expiry = Time.at(self.expires_in)
  87. logger.debug "#{expiry}"
  88. return true if expiry < Time.now
  89. token_expires_at = expiry
  90. save if changed?
  91. false
  92. end
  93. end
  94.  
  95. @response_mess = http.request(req)
  96.  
  97. Exchanging an authorization code for a token
  98.  
  99. The application sends the code, along with its ID and password, in a POST request.
  100. POST /token HTTP/1.1
  101. Host: oauth.yandex.
  102. Content-type: application/x-www-form-urlencoded
  103. Content-Length: <length of request body>
  104. [Authorization: Basic <encoded client_id:client_secret string>]
  105.  
  106. grant_type=authorization_code
  107. & code=<authorization code>
  108. [& client_id=<application ID>]
  109. [& client_secret=<application password>]
  110. [& device_id=<device ID>]
  111. [& device_name=<device name>]
  112.  
  113. {"error_description": "Code has expired", "error": "invalid_grant"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement