Class: Sdkey::Client
- Inherits:
-
Object
- Object
- Sdkey::Client
- Defined in:
- lib/sdkey/client.rb
Overview
SDKey license client (sealed session protocol).
Flow: init (session handshake) → sealed validate / register / login / upgrade.
Sealed RPCs call init automatically when no session exists.
Instance Method Summary collapse
-
#clear_session ⇒ Object
Drop the current session (next sealed RPC will re-init).
- #init ⇒ Object
-
#initialize(api_base_url:, app_id:, app_version:, app_public_key_b64:, http_post: nil) ⇒ Client
constructor
A new instance of Client.
- #login(username:, password:, hwid: nil) ⇒ Object
- #register(username:, password:, email: nil, license_key: nil, hwid: nil) ⇒ Object
-
#session ⇒ Object
(also: #get_session)
Active session, if any.
- #upgrade(username:, license_key:, hwid: nil) ⇒ Object
- #validate(license_key, hwid = nil) ⇒ Object
Constructor Details
#initialize(api_base_url:, app_id:, app_version:, app_public_key_b64:, http_post: nil) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 29 |
# File 'lib/sdkey/client.rb', line 21 def initialize(api_base_url:, app_id:, app_version:, app_public_key_b64:, http_post: nil) @api_base_url = api_base_url.to_s.sub(%r{/+\z}, "") @app_id = app_id @app_version = app_version @app_public_key_b64 = app_public_key_b64 @http_post = http_post || method(:default_http_post) @public_key = nil @session = nil end |
Instance Method Details
#clear_session ⇒ Object
Drop the current session (next sealed RPC will re-init).
38 39 40 |
# File 'lib/sdkey/client.rb', line 38 def clear_session @session = nil end |
#init ⇒ Object
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sdkey/client.rb', line 42 def init @public_key = Crypto::Seal.import_public_key(@app_public_key_b64) client_nonce = SecureRandom.random_bytes(Crypto::Constants::CLIENT_NONCE_BYTES) begin status, body = @http_post.call( "#{@api_base_url}/api/v1/session/init", { "appId" => @app_id, "clientNonceB64" => Crypto::Encoding.bytes_to_base64(client_nonce), "clientVersion" => @app_version } ) rescue StandardError => e raise Error.new("NETWORK", "session init request failed", e) end if status < 200 || status >= 300 || !body["success"] raise Error.new( (body["code"] || "INIT_FAILED").to_s, (body["error"] || "session init failed").to_s ) end hello = { "appId" => @app_id, "hkdfSaltB64" => body["hkdfSaltB64"], "serverNonceB64" => body["serverNonceB64"], "sessionId" => body["sessionId"], "timestamp" => body["timestamp"], "v" => Crypto::Constants::PROTOCOL_VERSION } unless Crypto::Seal.verify_signature(@public_key, hello, body["signatureB64"]) raise Error.new("HELLO_SIGNATURE_INVALID", "hello signature verification failed") end aes_key = Crypto::Seal.derive_session_aes_key( client_nonce: client_nonce, server_nonce: Crypto::Encoding.base64_to_bytes(body["serverNonceB64"]), salt_b64: body["hkdfSaltB64"], app_id: @app_id ) @session = SessionState.new( session_id: body["sessionId"], aes_key: aes_key, server_nonce_b64: body["serverNonceB64"], hkdf_salt_b64: body["hkdfSaltB64"] ) @session end |
#login(username:, password:, hwid: nil) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/sdkey/client.rb', line 144 def login(username:, password:, hwid: nil) fields = { "username" => username, "password" => password } fields["hwid"] = hwid unless hwid.nil? client_auth("login", fields) end |
#register(username:, password:, email: nil, license_key: nil, hwid: nil) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sdkey/client.rb', line 133 def register(username:, password:, email: nil, license_key: nil, hwid: nil) fields = { "username" => username, "password" => password } fields["email"] = email unless email.nil? fields["licenseKey"] = license_key unless license_key.nil? fields["hwid"] = hwid unless hwid.nil? client_auth("register", fields) end |
#session ⇒ Object Also known as: get_session
Active session, if any.
32 33 34 |
# File 'lib/sdkey/client.rb', line 32 def session @session end |
#upgrade(username:, license_key:, hwid: nil) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/sdkey/client.rb', line 153 def upgrade(username:, license_key:, hwid: nil) fields = { "username" => username, "licenseKey" => license_key } fields["hwid"] = hwid unless hwid.nil? client_auth("upgrade", fields) end |
#validate(license_key, hwid = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/sdkey/client.rb', line 95 def validate(license_key, hwid = nil) inner = { "licenseKey" => license_key, "nonce" => Crypto::Encoding.bytes_to_base64( SecureRandom.random_bytes(Crypto::Constants::VALIDATE_NONCE_BYTES) ), "timestamp" => Time.now.to_i, "v" => Crypto::Constants::PROTOCOL_VERSION } if hwid # Keep lexicographic key order for the sealed inner JSON. inner = { "hwid" => hwid, "licenseKey" => inner["licenseKey"], "nonce" => inner["nonce"], "timestamp" => inner["timestamp"], "v" => inner["v"] } end plaintext = sealed_rpc("licenses/validate", inner) clear_session if plaintext["code"] == "SESSION_EXPIRED" tier_raw = plaintext["subscriptionTier"] subscription_tier = tier_raw.nil? ? nil : tier_raw.to_i ValidateResult.new( success: !!plaintext["success"], code: plaintext["code"].to_s, message: plaintext["message"].to_s, status: plaintext["status"], expires_at: plaintext["expiresAt"], subscription_tier: subscription_tier, timestamp: plaintext["timestamp"].to_i ) end |