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"
]
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"]
= {
'x-diuit-application-id' => app_id,
'x-diuit-app-key' => app_key
}
res = RestClient.get('https://api.diuit.net/1/auth/nonce', )
nonce = (JSON.parse(res))["nonce"]
= {
"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',
request_data = {
'jwt' => token,
'deviceId' => device_id,
'platform' => platform
}
= {
'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,
end
|