duo_universal 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ddc25daa62e34539d8d7b5da2b8fe454eb2fc7c848b72bf7098b2e3e1a0c8b7
4
+ data.tar.gz: a9859887f7b7e396c0d7a175fb3c49aea51407216d9228b67c0c6368f8054c25
5
+ SHA512:
6
+ metadata.gz: 3daa95110c9d941dfbf79a64401661013643023234595fb2b0fa9d0c27d9dcc1fdfb3e93544b8eb8d9430d9b39a3d3c766e52019fe389998bcf9fe4bf1ae3232
7
+ data.tar.gz: f1d95e2b5916a2a382d42208693269db122a1ebdeaa751d94fe0d1498fc4e3f2cbfea1406ce6c8326e00ed7f26220eb86dbb169c419ad420b7ac96652992eb5b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-19
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in duo.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ duo_universal (0.1.0.pre)
5
+ httparty
6
+ jwt
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ byebug (11.1.3)
13
+ diff-lcs (1.5.0)
14
+ httparty (0.20.0)
15
+ mime-types (~> 3.0)
16
+ multi_xml (>= 0.5.2)
17
+ jwt (2.4.1)
18
+ mime-types (3.4.1)
19
+ mime-types-data (~> 3.2015)
20
+ mime-types-data (3.2022.0105)
21
+ multi_xml (0.6.0)
22
+ parallel (1.21.0)
23
+ parser (3.1.1.0)
24
+ ast (~> 2.4.1)
25
+ rainbow (3.1.1)
26
+ rake (13.0.6)
27
+ regexp_parser (2.2.1)
28
+ rexml (3.2.5)
29
+ rspec (3.11.0)
30
+ rspec-core (~> 3.11.0)
31
+ rspec-expectations (~> 3.11.0)
32
+ rspec-mocks (~> 3.11.0)
33
+ rspec-core (3.11.0)
34
+ rspec-support (~> 3.11.0)
35
+ rspec-expectations (3.11.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-mocks (3.11.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.11.0)
41
+ rspec-support (3.11.0)
42
+ rubocop (1.25.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.1.0.0)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml
48
+ rubocop-ast (>= 1.15.1, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 3.0)
51
+ rubocop-ast (1.16.0)
52
+ parser (>= 3.1.1.0)
53
+ ruby-progressbar (1.11.0)
54
+ unicode-display_width (2.1.0)
55
+
56
+ PLATFORMS
57
+ x86_64-linux
58
+
59
+ DEPENDENCIES
60
+ byebug
61
+ duo_universal!
62
+ rake (~> 13.0)
63
+ rspec (~> 3.0)
64
+ rubocop (~> 1.21)
65
+
66
+ BUNDLED WITH
67
+ 2.3.20
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Andrew Walter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Duo
2
+ _A port of Duo's Universal Python SDK client to Ruby._
3
+
4
+ Python SDK: https://github.com/duosecurity/duo_universal_python
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add duo_universal
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install duo_universal
15
+
16
+ ## Usage
17
+
18
+ TODO: Write usage instructions here
19
+
20
+ ## Development
21
+
22
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
23
+
24
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
25
+
26
+ ## Contributing
27
+
28
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/duo.
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,175 @@
1
+ require 'jwt'
2
+ require 'securerandom'
3
+ require 'httparty'
4
+
5
+ require 'byebug'
6
+
7
+ module Duo
8
+ class Client
9
+ STATE_LENGTH = 36
10
+ JTI_LENGTH = 36
11
+ MINIMUM_STATE_LENGTH = 22
12
+ MAXIMUM_STATE_LENGTH = 1024
13
+ CLIENT_ID_LENGTH = 20
14
+ CLIENT_SECRET_LENGTH = 40
15
+ CLIENT_ASSERTION_TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'
16
+ JWT_LEEWAY = 60
17
+
18
+ attr_reader :client_id, :client_secret, :host, :redirect_uri, :use_duo_code_attribute
19
+
20
+ def initialize(client_id, client_secret, host, redirect_uri, optional_args = {})
21
+ raise Duo::ClientIDLengthError unless client_id && client_id.length == CLIENT_ID_LENGTH
22
+ raise Duo::ClientSecretLengthError unless client_secret && client_secret.length == CLIENT_SECRET_LENGTH
23
+ raise Duo::ApiHostRequiredError unless host
24
+ raise Duo::RedirectUriRequiredError unless redirect_uri
25
+
26
+ @client_id = client_id
27
+ @client_secret = client_secret
28
+ @host = host
29
+ @redirect_uri = redirect_uri
30
+ @use_duo_code_attribute = optional_args.fetch(:use_duo_code_attribute) { true }
31
+ end
32
+
33
+ def api_host_uri
34
+ "https://#{host}"
35
+ end
36
+
37
+ def authorize_endpoint_uri
38
+ "https://#{host}/oauth/v1/authorize"
39
+ end
40
+
41
+ def health_check_endpoint_uri
42
+ "https://#{host}/oauth/v1/health_check"
43
+ end
44
+
45
+ def token_endpoint_uri
46
+ "https://#{host}/oauth/v1/token"
47
+ end
48
+
49
+ def create_auth_url(username, state)
50
+ raise Duo::StateLengthError unless state && state.length >= MINIMUM_STATE_LENGTH && state.length <= MAXIMUM_STATE_LENGTH
51
+ raise Duo::UsernameRequiredError unless username && username.gsub(/\s*/, '').length > 0
52
+
53
+ jwt_args = {
54
+ scope: 'openid',
55
+ redirect_uri: redirect_uri,
56
+ client_id: client_id,
57
+ iss: client_id,
58
+ aud: api_host_uri,
59
+ exp: (Time.now + (5*60)).to_i,
60
+ state: state,
61
+ response_type: 'code',
62
+ duo_uname: username,
63
+ use_duo_code_attribute: use_duo_code_attribute,
64
+ }
65
+
66
+ req_jwt = JWT.encode(jwt_args, client_secret, 'HS512')
67
+
68
+ all_args = {
69
+ response_type: 'code',
70
+ client_id: client_id,
71
+ request: req_jwt
72
+ }
73
+
74
+ query_string = URI.encode_www_form all_args
75
+
76
+ "#{authorize_endpoint_uri}?#{query_string}"
77
+ end
78
+
79
+ def generate_state
80
+ SecureRandom.alphanumeric(STATE_LENGTH)
81
+ end
82
+
83
+ # Checks whether Duo is available.
84
+ # Returns:
85
+ # {'response': {'timestamp': <int:unix timestamp>}, 'stat': 'OK'}
86
+ # Raises:
87
+ #
88
+ def health_check
89
+ req_payload = {
90
+ client_assertion: JWT.encode(jwt_args_for(health_check_endpoint_uri), client_secret, 'HS512'),
91
+ client_id: client_id
92
+ }
93
+
94
+ # ToDo: Add Support for verifying SSL certificates
95
+ begin
96
+ res = HTTParty.post(health_check_endpoint_uri, body: req_payload)
97
+
98
+ json_resp = JSON.parse res.body
99
+
100
+ raise Duo::Error.new(json_resp) unless json_resp['stat'] == 'OK'
101
+
102
+ json_resp
103
+ rescue => e
104
+ raise e
105
+ end
106
+ end
107
+
108
+ # Exchanges the duo_code for a token with Duo to determine
109
+ # if the auth was successful.
110
+ # Argument:
111
+ # duo_code -- Authentication session transaction id
112
+ # returned by Duo
113
+ # username -- Name of the user authenticating with Duo
114
+ # nonce -- Random 36B string used to associate
115
+ # a session with an ID token
116
+ # Returns:
117
+ # A token with meta-data about the auth
118
+ # Raises:
119
+ # Duo::Error on error for invalid duo_codes, invalid credentials,
120
+ # or problems connecting to Duo
121
+ def exchange_authorization_code_for_2fa_result(duo_code, username, nonce = nil)
122
+ raise Duo::DuoCodeRequiredError unless duo_code
123
+
124
+ jwt_args = jwt_args_for(token_endpoint_uri)
125
+
126
+ all_args = {
127
+ grant_type: 'authorization_code',
128
+ code: duo_code,
129
+ redirect_uri: redirect_uri,
130
+ client_id: client_id,
131
+ client_assertion_type: CLIENT_ASSERTION_TYPE,
132
+ client_assertion: JWT.encode(jwt_args, client_secret, 'HS512')
133
+ }
134
+
135
+ begin
136
+ user_agent = "duo_universal_ruby/#{Duo::VERSION} ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}"
137
+
138
+ resp = HTTParty.post(token_endpoint_uri, body: all_args, headers: { user_agent: user_agent })
139
+
140
+ json_response_body = JSON.parse(resp.body)
141
+
142
+ raise Duo::Error.new(json_response_body) unless resp.code == 200
143
+
144
+ decoded_token = JWT.decode(json_response_body['id_token'], client_secret, true, {
145
+ algorithm: 'HS512',
146
+ iss: token_endpoint_uri,
147
+ verify_iss: true,
148
+ aud: client_id,
149
+ verify_aud: true,
150
+ exp_leeway: JWT_LEEWAY,
151
+ required_claims: ['exp', 'iat'],
152
+ verify_iat: false
153
+ })
154
+
155
+ # ToDo: finalise validation
156
+
157
+ decoded_token
158
+ rescue => e
159
+ raise Duo::Error.new(e.message)
160
+ end
161
+ end
162
+
163
+ private
164
+
165
+ def jwt_args_for(endpoint_uri)
166
+ {
167
+ iss: client_id,
168
+ sub: client_id,
169
+ aud: endpoint_uri,
170
+ exp: (Time.now + (5*60)).to_i,
171
+ jti: SecureRandom.alphanumeric(JTI_LENGTH)
172
+ }
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Duo
4
+ VERSION = "0.1.0.pre"
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "duo_universal/version"
4
+ require_relative "duo_universal/client"
5
+
6
+ module Duo
7
+ class Error < StandardError; end
8
+ class StateLengthError < StandardError; end
9
+ class UsernameRequiredError < StandardError; end
10
+ class ClientIDLengthError < StandardError; end
11
+ class ClientSecretLengthError < StandardError; end
12
+ class ApiHostRequiredError < StandardError; end
13
+ class RedirectUriRequiredError < StandardError; end
14
+ class DuoCodeRequiredError < StandardError; end
15
+ end
data/sig/duo.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Duo
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duo_universal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Walter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - andrew.walter@burnet.edu.au
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/duo_universal.rb
71
+ - lib/duo_universal/client.rb
72
+ - lib/duo_universal/version.rb
73
+ - sig/duo.rbs
74
+ homepage: https://www.github.com/xtrasimplicity/duo_universal_ruby
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://www.github.com/xtrasimplicity/duo_universal_ruby
79
+ source_code_uri: https://www.github.com/xtrasimplicity/duo_universal_ruby
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">"
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.1
94
+ requirements: []
95
+ rubygems_version: 3.0.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Duo Universal Ruby SDK
99
+ test_files: []