Class: Authing::Client
- Inherits:
-
Object
- Object
- Authing::Client
- Defined in:
- lib/authing.rb
Constant Summary collapse
- @@TokenMethods =
Set["getClientWhenSdkInit", "login"]
Instance Method Summary collapse
- #converter(moduleName, variables) ⇒ Object
- #encrypt(passwd) ⇒ Object
- #getconn ⇒ Object
- #getQuery(moduleName) ⇒ Object
- #getQueryName(moduleName) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #refrashToken ⇒ Object
- #request(moduleName, variables) ⇒ Object
- #setToken(result) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/authing.rb', line 14 def initialize(opts = {}) ["host", "publicKey", "userPoolId", "secret", "token"].each do |attr| instance_variable_set("@#{attr}", opts[attr]) end #把 opts 中的值替换成目标值 @host = HOST unless @host @publicKey = PUBLICKEY unless @publicKey self.refrashToken if @userPoolId and @secret Static.constants.each do |attr| self.class.send(:define_method, Static.const_get(attr)::Name) { |variables = {}| return self.request( attr, variables ) } end end |
Instance Method Details
#converter(moduleName, variables) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/authing.rb', line 62 def converter(moduleName, variables) variables = Util::userPoolIdConverter(getQuery(moduleName), variables, @userPoolId) if (moduleName.to_s == "Login" or moduleName.to_s == "ChangePassword") and variables[:password] variables["password"] = self.encrypt(variables[:password]) elsif (moduleName.to_s == "Register") variables[:userInfo]["password"] = self.encrypt(variables[:userInfo][:password]) end return variables end |
#encrypt(passwd) ⇒ Object
40 41 42 43 44 |
# File 'lib/authing.rb', line 40 def encrypt(passwd) rsa = OpenSSL::PKey::RSA.new @publicKey rsa.public_encrypt(passwd) return Base64.urlsafe_encode64(rsa.public_encrypt(passwd)) end |
#getconn ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/authing.rb', line 30 def getconn() return Faraday.new( url: HOST, headers: { "Authorization" => "Bearer #{@token}", "Content-Type" => "application/json", }, ) end |
#getQuery(moduleName) ⇒ Object
77 78 79 |
# File 'lib/authing.rb', line 77 def getQuery(moduleName) return Static.const_get(moduleName)::Query end |
#getQueryName(moduleName) ⇒ Object
73 74 75 |
# File 'lib/authing.rb', line 73 def getQueryName(moduleName) return Static.const_get(moduleName)::Name end |
#refrashToken ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/authing.rb', line 46 def refrashToken() if @userPoolId and @secret self.setToken(self.request("GetClientWhenSdkInit", { "secret" => @secret, "clientId" => @userPoolId, })) else @token = nil end end |
#request(moduleName, variables) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/authing.rb', line 81 def request(moduleName, variables) self.refrashToken if Util::checkToken(@token) if @token variables = converter(moduleName, variables) resp = self.getconn.post() do |req| req.body = { query: getQuery(moduleName).to_s, variables: variables.to_json, }.to_json end resp = JSON.parse resp.body if resp["errors"] return resp end result = resp["data"][getQueryName(moduleName)] self.setToken(result) unless @token if @@TokenMethods.include?(getQueryName(moduleName)) return result end |
#setToken(result) ⇒ Object
57 58 59 60 |
# File 'lib/authing.rb', line 57 def setToken(result) @token = result["accessToken"] if result["accessToken"] @token = result["token"] if result["token"] end |