nuid-sdk 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d870c8df47c135af740aa98e693bfe2fea401eb1014002a719ea05d518bca0eb
4
- data.tar.gz: f52d91f7d14c678daa906c173e1c5a058fbb4d952e132521aee32019d4a3944b
3
+ metadata.gz: f1036c9267f051f3b984ee91f0d11445769268e8a005e9614ee8d657beec9624
4
+ data.tar.gz: 877e2cc137cb7143aad46f69a8b5b6b198b5b7fee2675f394276f94ebcfee038
5
5
  SHA512:
6
- metadata.gz: a7c35e9a6771a5c9b994c0e64844237f3948374343ee3941b72819b9f8f666cea1750078f2ac61ecfc4ca263d89e7b83c7c68bdc7ac2524a36acb22dfc0cbe6d
7
- data.tar.gz: 64b7386e55889c4a597866e07661ced6008fc06abb65bb81977f65d83fbb21e1cb6713e993838e3f6fce511669e64a9f62c8038f50a10f324892de6b9aba16e7
6
+ metadata.gz: 6d5708dd3898dda0b8c0b943e6e77fc4e95dded0aa2123891690fb7971216cfb43ee035d1f057191dc780523a9354f606af302bb722c5df17bb694789a5c050f
7
+ data.tar.gz: '0606849f71c9611b5724b62bf9006b4f311f140afb013aab0854a0ecb08487ace05a5f6ee899399e7f2a0cd7cb484df2e8b734a1b87d2f2b207e19c0fef181f7'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nuid-sdk (0.1.1)
4
+ nuid-sdk (0.2.0)
5
5
  httparty (~> 0.18.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  # NuID SDK for Ruby
4
4
 
5
+ [![](https://img.shields.io/gem/v/nuid-sdk?color=red&logo=rubygems&style=for-the-badge)](https://rubygems.org/gems/nuid-sdk)
6
+ [![](https://img.shields.io/badge/docs-v0.2.0-blue?style=for-the-badge&logo=read-the-docs)](https://rubydoc.info/gems/nuid-sdk)
7
+ [![](https://img.shields.io/badge/docs-platform-purple?style=for-the-badge&logo=read-the-docs)](https://portal.nuid.io/docs)
8
+
5
9
  This repo provides a Ruby Gem for interacting with NuID APIs within Ruby
6
10
  applications.
7
11
 
@@ -15,14 +19,14 @@ video tutorials, and more.
15
19
  From [rubygems](https://rubygems.org/gems/nuid-sdk):
16
20
 
17
21
  ```sh
18
- gem install nuid-sdk -v "0.1.1"
22
+ gem install nuid-sdk -v "0.2.0"
19
23
  ```
20
24
 
21
25
  Or with bundler:
22
26
 
23
27
  ```ruby
24
28
  # Gemfile
25
- gem "nuid-sdk", "~> 0.1.1"
29
+ gem "nuid-sdk", "~> 0.2"
26
30
  ```
27
31
 
28
32
  ## Usage
@@ -34,9 +38,26 @@ NuID](https://portal.nuid.io/docs/guides/integrating-with-nuid) guide and the
34
38
  accompanying [examples repo](https://github.com/NuID/examples).
35
39
  A ruby-specific code example is coming soon.
36
40
 
37
- ```ruby
38
- require "nuid-sdk"
41
+ ``` ruby
42
+ # config/application.rb
43
+ config.x.nuid.auth_api_key = ENV['NUID_API_KEY']
44
+ ```
45
+
46
+ ``` ruby
47
+ require "nuid/sdk/api/auth"
39
48
 
49
+ class ApplicationController < ActionController::API
50
+ def nuid_api
51
+ @nuid_api ||= ::NuID::SDK::API::Auth.new(Rails.configuration.x.nuid.auth_api_key)
52
+ end
53
+
54
+ def render_error(error, status)
55
+ render(json: {errors: [error]}, status: status)
56
+ end
57
+ end
58
+ ```
59
+
60
+ ```ruby
40
61
  class UsersController < ApplicationController
41
62
  NUID_API = ::NuID::SDK::API::Auth.new(ENV["NUID_API_KEY"])
42
63
 
@@ -49,22 +70,26 @@ class UsersController < ApplicationController
49
70
  # using `Zk.verifiableFromSecret(password)` from the `@nuid/zk` npm
50
71
  # package.
51
72
  def register
52
- credential_res = NUID_API.credential_create(params[:verified_credential])
53
- if credential_res.ok?
54
- user_params = params.require(:email, :first_name, :last_name)
55
- .merge({nuid: credential_res.parsed_response["nu/id"]})
56
- @current_user = User.create(user_params)
57
- render json: @current_user, status: :created
58
- else
59
- render status: :bad_request
73
+ credential_res = nuid_api.credential_create(params[:credential])
74
+ unless credential_res.code == 201
75
+ return render_error("Unable to create the credential", :bad_request)
60
76
  end
77
+
78
+ user = User.create!({
79
+ email: params[:email].strip.downcase,
80
+ first_name: params[:firstName],
81
+ last_name: params[:lastName],
82
+ nuid: credential_res.parsed_response["nu/id"]
83
+ })
84
+
85
+ render(json: { user: user }, status: :created)
86
+ rescue => exception
87
+ render_error(exception.message, 500)
61
88
  end
62
89
  end
63
90
  ```
64
91
 
65
92
  ``` ruby
66
- require "nuid-sdk"
67
-
68
93
  class SessionsController < ApplicationController
69
94
  NUID_API = ::NuID::SDK::API::Auth.new(ENV["NUID_API_KEY"])
70
95
 
@@ -73,18 +98,24 @@ class SessionsController < ApplicationController
73
98
  # challenge has been fetched, return it to the client so a proof
74
99
  # can be generated from the challenge claims and the user's password.
75
100
  def login_challenge
76
- user = User.find(email: params[:email])
77
- return render(status: :unauthorized) unless user
78
-
79
- credential_res = NUID_API.credential_get(user.nuid)
80
- return render(status: :unauthorized) unless credential_res.ok?
101
+ user = User.where(email: params[:email].strip.downcase).first
102
+ return render_error("User not found", :unauthorized) unless user
103
+
104
+ credential_res = nuid_api.credential_get(user.nuid)
105
+ unless credential_res.code == 200
106
+ return render_error("Credential not found", :unauthorized)
107
+ end
81
108
 
82
109
  credential = credential_res.parsed_response["nuid/credential"]
83
- challenge_res = NUID_API.challenge_get(credential)
84
- return render(status: :unauthorized) unless credential_res.ok?
110
+ challenge_res = nuid_api.challenge_get(credential)
111
+ unless challenge_res.code == 201
112
+ return render_error("Cannot create a challenge", 500)
113
+ end
85
114
 
86
115
  challenge_jwt = challenge_res.parsed_response["nuid.credential.challenge/jwt"]
87
- render json: {challenge_jwt: challenge_jwt}
116
+ render(json: { challengeJwt: challenge_jwt }, status: :ok)
117
+ rescue => exception
118
+ render_error(exception.message, 500)
88
119
  end
89
120
 
90
121
  # Verify is the second part of the login process. The params
@@ -97,17 +128,17 @@ class SessionsController < ApplicationController
97
128
  # `Zk.proofFromSecretAndChallenge(password, challenge_jwt)` from the
98
129
  # `@nuid/zk` npm package.
99
130
  def login_verify
100
- user = User.find(email: params[:email])
101
- return render(status: :unauthorized) unless user
102
-
103
- verify_res = NUID_API.challenge_verify(params[:challenge_jwt], params[:proof])
104
- if res.ok?
105
- @current_user = user
106
- # issue session ...
107
- render(json: @current_user)
108
- else
109
- render(status: :unathorized)
131
+ user = User.where(email: params[:email].strip.downcase).first
132
+ return render_error("User not found", :unauthorized) unless user
133
+
134
+ challenge_res = nuid_api.challenge_verify(params[:challengeJwt], params[:proof])
135
+ unless challenge_res.code == 200
136
+ return render_error("Verification failed", :unauthorized)
110
137
  end
138
+
139
+ render(json: { user: user }, status: :ok)
140
+ rescue => exception
141
+ render_error(exception.message, 500)
111
142
  end
112
143
  end
113
144
  ```
data/lib/nuid-sdk.rb ADDED
@@ -0,0 +1 @@
1
+ require "nuid/sdk"
@@ -6,11 +6,9 @@ module NuID::SDK::API
6
6
  # existing authentication flows.
7
7
  #
8
8
  # @example User Registration
9
- # require "nuid-sdk"
10
- #
11
9
  # class UsersController < ApplicationController
12
10
  # NUID_API = ::NuID::SDK::API::Auth.new(ENV["NUID_API_KEY"])
13
- #
11
+ #
14
12
  # # The registration form should send the verified credential to be
15
13
  # # recorded in the NuID Auth API. The response to that interaction
16
14
  # # will provide a `nu/id` key in the response which should be stored
@@ -20,22 +18,25 @@ module NuID::SDK::API
20
18
  # # using `Zk.verifiableFromSecret(password)` from the `@nuid/zk` npm
21
19
  # # package.
22
20
  # def register
23
- # credential_res = NUID_API.credential_create(params[:verified_credential])
24
- # return render(status: :unauthorized) unless credential_res.ok?
25
- # if credential_res.ok?
26
- # user_params = params.require(:email, :first_name, :last_name)
27
- # .merge({nuid: credential_res.parsed_response["nu/id"]})
28
- # @current_user = User.create(user_params)
29
- # render json: @current_user, status: :created
30
- # else
31
- # render status: :bad_request
21
+ # credential_res = nuid_api.credential_create(params[:credential])
22
+ # unless credential_res.code == 201
23
+ # return render_error("Unable to create the credential", :bad_request)
32
24
  # end
25
+ #
26
+ # user = User.create!({
27
+ # email: params[:email].strip.downcase,
28
+ # first_name: params[:firstName],
29
+ # last_name: params[:lastName],
30
+ # nuid: credential_res.parsed_response["nu/id"]
31
+ # })
32
+ #
33
+ # render(json: { user: user }, status: :created)
34
+ # rescue => exception
35
+ # render_error(exception.message, 500)
33
36
  # end
34
37
  # end
35
38
  #
36
39
  # @example User Login
37
- # require "nuid-sdk"
38
- #
39
40
  # class SessionsController < ApplicationController
40
41
  # NUID_API = ::NuID::SDK::API::Auth.new(ENV["NUID_API_KEY"])
41
42
  #
@@ -44,20 +45,26 @@ module NuID::SDK::API
44
45
  # # challenge has been fetched, return it to the client so a proof
45
46
  # # can be generated from the challenge claims and the user's password.
46
47
  # def login_challenge
47
- # user = User.find(email: params[:email])
48
- # return render(status: :unauthorized) unless user
49
- #
50
- # credential_res = NUID_API.credential_get(user.nuid)
51
- # return render(status: :unauthorized) unless credential_res.ok?
52
- #
48
+ # user = User.where(email: params[:email].strip.downcase).first
49
+ # return render_error("User not found", :unauthorized) unless user
50
+ #
51
+ # credential_res = nuid_api.credential_get(user.nuid)
52
+ # unless credential_res.code == 200
53
+ # return render_error("Credential not found", :unauthorized)
54
+ # end
55
+ #
53
56
  # credential = credential_res.parsed_response["nuid/credential"]
54
- # challenge_res = NUID_API.challenge_get(credential)
55
- # return render(status: :unauthorized) unless credential_res.ok?
56
- #
57
+ # challenge_res = nuid_api.challenge_get(credential)
58
+ # unless challenge_res.code == 201
59
+ # return render_error("Cannot create a challenge", 500)
60
+ # end
61
+ #
57
62
  # challenge_jwt = challenge_res.parsed_response["nuid.credential.challenge/jwt"]
58
- # render json: {challenge_jwt: challenge_jwt}
63
+ # render(json: { challengeJwt: challenge_jwt }, status: :ok)
64
+ # rescue => exception
65
+ # render_error(exception.message, 500)
59
66
  # end
60
- #
67
+ #
61
68
  # # Verify is the second part of the login process. The params
62
69
  # # provided here include the user identification param (email or
63
70
  # # username), the unaltered challenge_jwt retrieved in phase 1 of login
@@ -68,21 +75,20 @@ module NuID::SDK::API
68
75
  # # `Zk.proofFromSecretAndChallenge(password, challenge_jwt)` from the
69
76
  # # `@nuid/zk` npm package.
70
77
  # def login_verify
71
- # user = User.find(email: params[:email])
72
- # return render(status: :unauthorized) unless user
73
- #
74
- # verify_res = NUID_API.challenge_verify(params[:challenge_jwt], params[:proof])
75
- # if res.ok?
76
- # @current_user = user
77
- # # issue session ...
78
- # render(json: @current_user)
79
- # else
80
- # render(status: :unathorized)
78
+ # user = User.where(email: params[:email].strip.downcase).first
79
+ # return render_error("User not found", :unauthorized) unless user
80
+ #
81
+ # challenge_res = nuid_api.challenge_verify(params[:challengeJwt], params[:proof])
82
+ # unless challenge_res.code == 200
83
+ # return render_error("Verification failed", :unauthorized)
81
84
  # end
85
+ #
86
+ # render(json: { user: user }, status: :ok)
87
+ # rescue => exception
88
+ # render_error(exception.message, 500)
82
89
  # end
83
90
  # end
84
91
  #
85
- #
86
92
  # @see https://www.npmjs.com/package/@nuid/zk
87
93
  # @see https://www.npmjs.com/package/@nuid/cli
88
94
  class Auth
@@ -1,5 +1,5 @@
1
1
  module NuID
2
2
  module SDK
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nuid-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BJ Neilsen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-05 00:00:00.000000000 Z
11
+ date: 2021-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -56,6 +56,7 @@ files:
56
56
  - Rakefile
57
57
  - bin/console
58
58
  - bin/setup
59
+ - lib/nuid-sdk.rb
59
60
  - lib/nuid/sdk.rb
60
61
  - lib/nuid/sdk/api/auth.rb
61
62
  - lib/nuid/sdk/version.rb