Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. require 'httparty'
  2. require 'securerandom'
  3. require 'jwt'
  4. require 'json'
  5. require 'pp'
  6.  
  7. # Basic functionality to create accounts and login via Verimi
  8. class VerimiAPI
  9. attr_reader :base_uri, :grant_type, :redirect_uri, :client_id, :client_secret
  10.  
  11. def initialize
  12. @base_uri = URI.parse(ENV['verimi_base_url'])
  13. @grant_type = 'authorization_code'
  14. @redirect_uri = ENV['verimi_redirection_url']
  15. @client_id = ENV['verimi_client_id']
  16. @client_secret = ENV['verimi_secret_id']
  17. end
  18.  
  19. # From client Verimi login code extracts client credentials for account processing
  20. def self.load_credentials(code)
  21. @api ||= VerimiAPI.new
  22. @api.load_credentials(code)
  23. end
  24.  
  25. def load_credentials(code)
  26. verimi_token = get_verimi_token(code)
  27. credentials = get_verimi_credentials(verimi_token)
  28. return nil if credentials.nil?
  29. process_verimi_credentials(credentials[:email], credentials[:first_name], credentials[:last_name], credentials[:verimi_id])
  30. end
  31.  
  32. # Confirms to Verimi user was successfully logged in
  33. def login_verimi(euid)
  34. VerimiHttpClient.put("#{base_uri}users/#{euid}/linkage",
  35. options: {
  36. headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
  37. },
  38. basic_auth: {
  39. username: client_id,
  40. password: client_secret
  41. })
  42. end
  43.  
  44. # Informs Verimi current user is disconnected from platform
  45. def logout_verimi(euid)
  46. VerimiHttpClient.delete("#{base_uri}users/#{euid}/linkage",
  47. options: {
  48. headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
  49. },
  50. basic_auth: {
  51. username: client_id,
  52. password: client_secret
  53. })
  54. end
  55.  
  56. private
  57.  
  58. # Return login credentials, creating user in system whenever it is possible
  59. # to read basket from Verimi. In case no credentials are found and basket is
  60. # not accessible it returns null
  61. def get_verimi_credentials(verimi_token)
  62. verimi_user_id = JWT.decode(verimi_token['id_token'], nil, false).first['eeuid']
  63. verimi_user = User.where(verimi_id: verimi_user_id).first
  64. request_scope = verimi_token['scope']
  65. access_token = verimi_token['access_token']
  66.  
  67. if (request_scope.include? 'read_basket')
  68. get_baskets(access_token).merge(verimi_id: verimi_user_id)
  69. else
  70. return nil if verimi_user.nil?
  71. verimi_user.attributes.deep_symbolize_keys.merge(verimi_id: verimi_user_id)
  72. end
  73. end
  74.  
  75. # Request Verimi API access token from user login code
  76. def get_verimi_token(code)
  77. # Get client tokens from Verimi
  78. verimi_token_response = VerimiHttpClient.post("#{base_uri}oauth/token?grant_type=#{grant_type}&redirect_uri=#{redirect_uri}&code=#{code}",
  79. options: {
  80. headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
  81. },
  82. basic_auth: {
  83. username: client_id,
  84. password: client_secret
  85. })
  86.  
  87. raise "error when requesting access token" if verimi_token_response.code >= 300
  88. verimi_token_response.parsed_response
  89. end
  90.  
  91. # Request access to client Baskets after successful verimi login, in order to request user credentials
  92. def get_baskets(access_token)
  93. # get list of baskets
  94. headers = {
  95. "Authorization" => "Bearer #{access_token}",
  96. "Content-Type" => "application/json" }
  97. basket_response = VerimiHttpClient.get("#{base_uri}query/baskets",headers: headers)
  98.  
  99. raise "error when requesting basket" if basket_response.code >= 300
  100.  
  101. # look for specific information in basket
  102. basket = basket_response.parsed_response
  103. {
  104. first_name: basket.dig('dataScopes')[1].dig('data')[0].dig('value'),
  105. last_name: basket.dig('dataScopes')[1].dig('data')[1].dig('value'),
  106. email: basket.dig('dataScopes')[0].dig('data')[0].dig('value')
  107. } if !basket.nil?
  108. end
  109.  
  110. # From Verimi credentials checks whether user exists or not for creating user account and returning credentials
  111. def process_verimi_credentials(email, first_name, last_name, verimi_id)
  112. verimi_user_password = SecureRandom.base64(20)
  113. verimi_user = User.where(email: email).first_or_create do |user|
  114. user.password = verimi_user_password
  115. user.password_confirmation = verimi_user_password
  116. user.first_name = first_name
  117. user.last_name = last_name
  118. user.email = email
  119. user.verimi_id = verimi_id
  120. end
  121.  
  122. verimi_user.update({password: verimi_user_password, password_confirmation: verimi_user_password})
  123.  
  124. { email: email, password: verimi_user_password }
  125. end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement