Advertisement
Guest User

Untitled

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