Class: Diuitauth::Login

Inherits:
Object
  • Object
show all
Defined in:
lib/diuitauth.rb

Overview

Your code goes hereā€¦

Class Method Summary collapse

Class Method Details

.get_session_token(client) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/diuitauth.rb', line 8

def self.get_session_token(client)
  begin
    JSON.parse(client)
  rescue JSON::ParserError => e
    return "Invalid JSON format"
  end

  params = [
    "app_id",
    "app_key",
    "key_id",
    "private_key",
    "exp",
    "platform",
    "user_serial",
    "device_id"
  ]

  # check json data is enough or not
  params.each do |p|
    unless (JSON.parse(client)).has_key?(p)
      return "can not find #{p}"
    end
  end

  clientJSON = JSON.parse(client)
  app_id = clientJSON["app_id"]
  app_key = clientJSON["app_key"]
  kid = clientJSON["key_id"]
  private_key = clientJSON["private_key"]
  exp = clientJSON["exp"].to_i
  platform = clientJSON["platform"]
  sub = clientJSON["user_serial"]
  device_id = clientJSON["device_id"]

  header = {
    'x-diuit-application-id' => app_id,
    'x-diuit-app-key' => app_key
  }
  res = RestClient.get('https://api.diuit.net/1/auth/nonce', header)
  nonce = (JSON.parse(res))["nonce"]
  jwt_header = {
    "typ" => 'JWT',
    "alg" => 'RS256',
    "cty" => "diuit-auth;v=1",
    "kid" => kid
  }

  jwt_payload = {
    "exp" => Time.at(exp).utc.iso8601,
    "iss" => app_id,
    "iat" => Time.now.utc.iso8601,
    "sub" => sub,
    "nonce" => nonce
  }

  token = JWT.encode jwt_payload, private_key, 'none', jwt_header

  request_data = {
    'jwt' => token,
    'deviceId' => device_id,
    'platform' => platform
  }
  request_headers = {
    'x-diuit-application-id' => app_id,
    'x-diuit-app-key' => app_key,
    'Content-Type' => 'application/json'
  }
  
  return RestClient.post 'https://api.diuit.net/1/auth/login', request_data, request_headers
end

.TestObject



80
81
82
# File 'lib/diuitauth.rb', line 80

def self.Test
  puts "Test"
end