Class: NuID::SDK::API::Auth
- Inherits:
-
Object
- Object
- NuID::SDK::API::Auth
- Includes:
- HTTParty
- Defined in:
- lib/nuid/sdk/api/auth.rb
Overview
This class wraps the NuID Auth API endpoints for simpler integration into existing authentication flows.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#challenge_get(credential) ⇒ HTTParty::Response
Get a credential ‘challenge` from the API, usually during login flow.
-
#challenge_verify(challenge_jwt, proof) ⇒ HTTParty::Response
Verify a credential challenge with a proof generated from the challenge claims and the user’s secret.
-
#credential_create(verified_credential) ⇒ HTTParty::Response
Create a credential from a verified credential (meaning a credential generated from the user’s secret).
-
#credential_get(nuid) ⇒ HTTParty::Response
Fetch a credential by it’s unique ‘nuid`.
-
#initialize(api_key) ⇒ Auth
constructor
Create an HTTParty instance for dispatching HTTP requests.
Constructor Details
#initialize(api_key) ⇒ Auth
Create an HTTParty instance for dispatching HTTP requests.
All endpoints return the HTTParty Response object, with ‘HTTParty::Response#parsed_response` containing the JSON body converted to a hash.
108 109 110 111 112 113 114 |
# File 'lib/nuid/sdk/api/auth.rb', line 108 def initialize(api_key) @api_key = api_key self.class.headers({ "X-API-Key" => @api_key, "Accept" => "application/json" }) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
98 99 100 |
# File 'lib/nuid/sdk/api/auth.rb', line 98 def api_key @api_key end |
Instance Method Details
#challenge_get(credential) ⇒ HTTParty::Response
Get a credential ‘challenge` from the API, usually during login flow. The returned `challenge` can be used to generate a proof from the user’s secret. Used in conjunction with #challenge_verify.
128 129 130 |
# File 'lib/nuid/sdk/api/auth.rb', line 128 def challenge_get(credential) _post("/challenge", {"nuid/credential" => credential}) end |
#challenge_verify(challenge_jwt, proof) ⇒ HTTParty::Response
Verify a credential challenge with a proof generated from the challenge claims and the user’s secret. Generated proof from the claims contained in the ‘challenge_jwt` and the user’s secret. This proof is generated by ‘Zk.proofFromSecretAndChallenge(secret, challenge)` available in the npm package `@nuid/zk`.
147 148 149 150 151 152 |
# File 'lib/nuid/sdk/api/auth.rb', line 147 def challenge_verify(challenge_jwt, proof) _post("/challenge/verify", { "nuid.credential.challenge/jwt" => challenge_jwt, "nuid.credential/proof" => proof }) end |
#credential_create(verified_credential) ⇒ HTTParty::Response
Create a credential from a verified credential (meaning a credential generated from the user’s secret). Usually used during user registration. The parsed response body contains the new credential and the user’s unique “nu/id” which should be used as a reference to the user’s credential for later authentication attempts.
167 168 169 |
# File 'lib/nuid/sdk/api/auth.rb', line 167 def credential_create(verified_credential) _post("/credential", {"nuid.credential/verified" => verified_credential}) end |
#credential_get(nuid) ⇒ HTTParty::Response
Fetch a credential by it’s unique ‘nuid`. The `nu/id` paramter is extracted from the `#parsed_response` of #credential_create.
Generally you will end up storing the nuid with your user record during registration. Later during login use the nuid to fetch the credential using this method, and pass the Response#parsed_response directly to #challenge_get.
182 183 184 |
# File 'lib/nuid/sdk/api/auth.rb', line 182 def credential_get(nuid) self.class.get("/credential/#{nuid}") end |