net-http-sspi 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.mailmap +21 -0
- data/BSDL +22 -0
- data/COPYING +56 -0
- data/README.md +50 -0
- data/lib/win32/sspi.rb +339 -0
- data/rakelib/epoch.rake +5 -0
- data/sig/win32/sspi.rbs +6 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e20365ef4eaf3a469c477b1ba868280bf2c79c3060750273de64152d36064f8f
|
|
4
|
+
data.tar.gz: 3a1cc8c70bd4783e9675808fe1a5a2fd97c0de47befa98e70762223d7932208a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 48c9e4a3b8469638d60734f4725fb5527f2bec31043b4389560d22bd45a868dc12d30ab17f7fbd32bf0df8c7c34421465108af0e9f908b08f0ddabcfe8ec77d6
|
|
7
|
+
data.tar.gz: bc45ab63e507d5a8a1b5dfbbfea9362a4697e8eb7e91b6f6acf51a5d62788c1cd16f6660460b5ab58cab10b957554dedff60ef50c5ccec8652c0e38f6a93a908
|
data/.mailmap
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# aamine
|
|
2
|
+
Minero Aoki <aamine@loveruby.net>
|
|
3
|
+
Minero Aoki <aamine@loveruby.net> <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
|
|
4
|
+
|
|
5
|
+
# hsbt
|
|
6
|
+
Hiroshi SHIBATA <hsbt@ruby-lang.org>
|
|
7
|
+
Hiroshi SHIBATA <hsbt@ruby-lang.org> <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
|
|
8
|
+
|
|
9
|
+
# naruse
|
|
10
|
+
NARUSE, Yui <naruse@airemix.jp>
|
|
11
|
+
NARUSE, Yui <naruse@airemix.jp> <naruse@ruby-lang.org>
|
|
12
|
+
NARUSE, Yui <naruse@airemix.jp> <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
|
|
13
|
+
|
|
14
|
+
# nobu
|
|
15
|
+
Nobuyoshi Nakada <nobu@ruby-lang.org>
|
|
16
|
+
Nobuyoshi Nakada <nobu@ruby-lang.org> <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
|
|
17
|
+
|
|
18
|
+
# usa
|
|
19
|
+
U.Nakamura <usa@ruby-lang.org>
|
|
20
|
+
U.Nakamura <usa@ruby-lang.org> <usa@garbagecollect.jp>
|
|
21
|
+
U.Nakamura <usa@ruby-lang.org> <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
|
data/BSDL
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions
|
|
5
|
+
are met:
|
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
|
10
|
+
documentation and/or other materials provided with the distribution.
|
|
11
|
+
|
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
22
|
+
SUCH DAMAGE.
|
data/COPYING
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
|
|
2
|
+
You can redistribute it and/or modify it under either the terms of the
|
|
3
|
+
2-clause BSDL (see the file BSDL), or the conditions below:
|
|
4
|
+
|
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
|
6
|
+
software without restriction, provided that you duplicate all of the
|
|
7
|
+
original copyright notices and associated disclaimers.
|
|
8
|
+
|
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
|
10
|
+
you do at least ONE of the following:
|
|
11
|
+
|
|
12
|
+
a. place your modifications in the Public Domain or otherwise
|
|
13
|
+
make them Freely Available, such as by posting said
|
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
|
15
|
+
the author to include your modifications in the software.
|
|
16
|
+
|
|
17
|
+
b. use the modified software only within your corporation or
|
|
18
|
+
organization.
|
|
19
|
+
|
|
20
|
+
c. give non-standard binaries non-standard names, with
|
|
21
|
+
instructions on where to get the original software distribution.
|
|
22
|
+
|
|
23
|
+
d. make other distribution arrangements with the author.
|
|
24
|
+
|
|
25
|
+
3. You may distribute the software in object code or binary form,
|
|
26
|
+
provided that you do at least ONE of the following:
|
|
27
|
+
|
|
28
|
+
a. distribute the binaries and library files of the software,
|
|
29
|
+
together with instructions (in the manual page or equivalent)
|
|
30
|
+
on where to get the original distribution.
|
|
31
|
+
|
|
32
|
+
b. accompany the distribution with the machine-readable source of
|
|
33
|
+
the software.
|
|
34
|
+
|
|
35
|
+
c. give non-standard binaries non-standard names, with
|
|
36
|
+
instructions on where to get the original software distribution.
|
|
37
|
+
|
|
38
|
+
d. make other distribution arrangements with the author.
|
|
39
|
+
|
|
40
|
+
4. You may modify and include the part of the software into any other
|
|
41
|
+
software (possibly commercial). But some files in the distribution
|
|
42
|
+
are not written by the author, so that they are not under these terms.
|
|
43
|
+
|
|
44
|
+
For the list of those files and their copying conditions, see the
|
|
45
|
+
file LEGAL.
|
|
46
|
+
|
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
|
48
|
+
output from the software do not automatically fall under the
|
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
|
51
|
+
software.
|
|
52
|
+
|
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
56
|
+
PURPOSE.
|
data/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Win32::SSPI
|
|
2
|
+
|
|
3
|
+
win32-sspi implements bindings to Win32 SSPI functions, focused on
|
|
4
|
+
authentication to a proxy server over HTTP.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
win32-sspi is a bundled gem of the ruby standard library, so that the
|
|
9
|
+
latest version on the ruby release date is preinstalled on Windows.
|
|
10
|
+
Other versions of the gem can be installed in addition like so:
|
|
11
|
+
|
|
12
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
13
|
+
|
|
14
|
+
$ bundle add win32-sspi
|
|
15
|
+
|
|
16
|
+
If bundler is not being used to manage dependencies, install the gem
|
|
17
|
+
by executing:
|
|
18
|
+
|
|
19
|
+
$ gem install win32-sspi
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require "net/http"
|
|
25
|
+
|
|
26
|
+
proxy_arguments = [proxy_server,] # ...
|
|
27
|
+
http = Net::HTTP.new(hostname, nil, *proxy_arguments)
|
|
28
|
+
http.instance_variable_set(:@sspi_enabled, true)
|
|
29
|
+
|
|
30
|
+
req = Net::HTTP::Get.new('/todos/1')
|
|
31
|
+
http.request(req)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
See [Proxy Server in Net::HTTP] for proxy arguments.
|
|
35
|
+
|
|
36
|
+
[Proxy Server in Net::HTTP]: https://docs.ruby-lang.org/en/master/Net/HTTP.html#class-Net::HTTP-label-Proxy+Server
|
|
37
|
+
|
|
38
|
+
## Development
|
|
39
|
+
|
|
40
|
+
To install this gem onto your local machine, run `bundle exec rake
|
|
41
|
+
install`. To release a new version, update the version number in
|
|
42
|
+
`version.rb`, and then run `bundle exec rake release`, which will
|
|
43
|
+
create a git tag for the version, push git commits and the created
|
|
44
|
+
tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
45
|
+
|
|
46
|
+
## Contributing
|
|
47
|
+
|
|
48
|
+
Bug reports and pull requests are welcome on [GitHub].
|
|
49
|
+
|
|
50
|
+
[GitHub]: https://github.com/ruby/win32-sspi
|
data/lib/win32/sspi.rb
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
# frozen_string_literal: false
|
|
2
|
+
#
|
|
3
|
+
# = win32/sspi.rb
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2006-2007 Justin Bailey
|
|
6
|
+
#
|
|
7
|
+
# Written and maintained by Justin Bailey <jgbailey@gmail.com>.
|
|
8
|
+
#
|
|
9
|
+
# This program is free software. You can re-distribute and/or
|
|
10
|
+
# modify this program under the same terms of ruby itself ---
|
|
11
|
+
# Ruby Distribution License or GNU General Public License.
|
|
12
|
+
#
|
|
13
|
+
|
|
14
|
+
require 'fiddle/import'
|
|
15
|
+
|
|
16
|
+
# Implements bindings to Win32 SSPI functions, focused on authentication to a proxy server over HTTP.
|
|
17
|
+
module Win32
|
|
18
|
+
module SSPI
|
|
19
|
+
# Specifies how credential structure requested will be used. Only SECPKG_CRED_OUTBOUND is used
|
|
20
|
+
# here.
|
|
21
|
+
SECPKG_CRED_INBOUND = 0x00000001
|
|
22
|
+
SECPKG_CRED_OUTBOUND = 0x00000002
|
|
23
|
+
SECPKG_CRED_BOTH = 0x00000003
|
|
24
|
+
|
|
25
|
+
# Format of token. NETWORK format is used here.
|
|
26
|
+
SECURITY_NATIVE_DREP = 0x00000010
|
|
27
|
+
SECURITY_NETWORK_DREP = 0x00000000
|
|
28
|
+
|
|
29
|
+
# InitializeSecurityContext Requirement flags
|
|
30
|
+
ISC_REQ_REPLAY_DETECT = 0x00000004
|
|
31
|
+
ISC_REQ_SEQUENCE_DETECT = 0x00000008
|
|
32
|
+
ISC_REQ_CONFIDENTIALITY = 0x00000010
|
|
33
|
+
ISC_REQ_USE_SESSION_KEY = 0x00000020
|
|
34
|
+
ISC_REQ_PROMPT_FOR_CREDS = 0x00000040
|
|
35
|
+
ISC_REQ_CONNECTION = 0x00000800
|
|
36
|
+
|
|
37
|
+
# Win32 API Functions. Uses Win32API to bind methods to constants contained in class.
|
|
38
|
+
module API
|
|
39
|
+
extend Fiddle::Importer
|
|
40
|
+
dlload "secur32.dll"
|
|
41
|
+
[
|
|
42
|
+
# Can be called with AcquireCredentialsHandleA.call()
|
|
43
|
+
"unsigned long AcquireCredentialsHandleA(void *, void *, unsigned long, void *, void *, void *, void *, void *, void *)",
|
|
44
|
+
# Can be called with InitializeSecurityContextA.call()
|
|
45
|
+
"unsigned long InitializeSecurityContextA(void *, void *, void *, unsigned long, unsigned long, unsigned long, void *, unsigned long, void *, void *, void *, void *)",
|
|
46
|
+
# Can be called with DeleteSecurityContext.call()
|
|
47
|
+
"unsigned long DeleteSecurityContext(void *)",
|
|
48
|
+
# Can be called with FreeCredentialsHandle.call()
|
|
49
|
+
"unsigned long FreeCredentialsHandle(void *)"
|
|
50
|
+
].each do |fn|
|
|
51
|
+
cfunc = extern fn, :stdcall
|
|
52
|
+
const_set cfunc.name.intern, cfunc
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# SecHandle struct
|
|
57
|
+
class SecurityHandle
|
|
58
|
+
def upper
|
|
59
|
+
@struct.unpack1("x4L")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def lower
|
|
63
|
+
@struct.unpack1("L")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def to_p
|
|
67
|
+
@struct ||= "\0" * 8
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Some familiar aliases for the SecHandle structure
|
|
72
|
+
CredHandle = CtxtHandle = SecurityHandle
|
|
73
|
+
|
|
74
|
+
# TimeStamp struct
|
|
75
|
+
class TimeStamp
|
|
76
|
+
attr_reader :struct
|
|
77
|
+
|
|
78
|
+
def to_p
|
|
79
|
+
@struct ||= "\0" * 8
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Creates binary representations of a SecBufferDesc structure,
|
|
84
|
+
# including the SecBuffer contained inside.
|
|
85
|
+
class SecurityBuffer
|
|
86
|
+
|
|
87
|
+
SECBUFFER_TOKEN = 2 # Security token
|
|
88
|
+
|
|
89
|
+
TOKENBUFSIZE = 12288
|
|
90
|
+
SECBUFFER_VERSION = 0
|
|
91
|
+
|
|
92
|
+
def initialize(buffer = nil)
|
|
93
|
+
@buffer = buffer || "\0" * TOKENBUFSIZE
|
|
94
|
+
@bufferSize = @buffer.length
|
|
95
|
+
@type = SECBUFFER_TOKEN
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def bufferSize
|
|
99
|
+
unpack
|
|
100
|
+
@bufferSize
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def bufferType
|
|
104
|
+
unpack
|
|
105
|
+
@type
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def token
|
|
109
|
+
unpack
|
|
110
|
+
@buffer
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def to_p
|
|
114
|
+
# Assumption is that when to_p is called we are going to get a packed structure. Therefore,
|
|
115
|
+
# set @unpacked back to nil so we know to unpack when accessors are next accessed.
|
|
116
|
+
@unpacked = nil
|
|
117
|
+
# Assignment of inner structure to variable is very important here. Without it,
|
|
118
|
+
# will not be able to unpack changes to the structure. Alternative, nested unpacks,
|
|
119
|
+
# does not work (i.e. @struct.unpack("LLP12")[2].unpack("LLP12") results in "no associated pointer")
|
|
120
|
+
@sec_buffer ||= [@bufferSize, @type, @buffer].pack("LLP")
|
|
121
|
+
@struct ||= [SECBUFFER_VERSION, 1, @sec_buffer].pack("LLP")
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
private
|
|
125
|
+
|
|
126
|
+
# Unpacks the SecurityBufferDesc structure into member variables. We
|
|
127
|
+
# only want to do this once per struct, so the struct is deleted
|
|
128
|
+
# after unpacking.
|
|
129
|
+
def unpack
|
|
130
|
+
if ! @unpacked && @sec_buffer && @struct
|
|
131
|
+
@bufferSize, @type = @sec_buffer.unpack("LL")
|
|
132
|
+
@buffer = @sec_buffer.unpack1("x8P#{@bufferSize}")
|
|
133
|
+
@struct = nil
|
|
134
|
+
@sec_buffer = nil
|
|
135
|
+
@unpacked = true
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# SEC_WINNT_AUTH_IDENTITY structure
|
|
141
|
+
class Identity
|
|
142
|
+
SEC_WINNT_AUTH_IDENTITY_ANSI = 0x1
|
|
143
|
+
|
|
144
|
+
attr_accessor :user, :domain, :password
|
|
145
|
+
|
|
146
|
+
def initialize(user = nil, domain = nil, password = nil)
|
|
147
|
+
@user = user
|
|
148
|
+
@domain = domain
|
|
149
|
+
@password = password
|
|
150
|
+
@flags = SEC_WINNT_AUTH_IDENTITY_ANSI
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def to_p
|
|
154
|
+
[@user, @user ? @user.length : 0,
|
|
155
|
+
@domain, @domain ? @domain.length : 0,
|
|
156
|
+
@password, @password ? @password.length : 0,
|
|
157
|
+
@flags].pack("PLPLPLL")
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Takes a return result from an SSPI function and interprets the value.
|
|
162
|
+
class SSPIResult
|
|
163
|
+
# Good results
|
|
164
|
+
SEC_E_OK = 0x00000000
|
|
165
|
+
SEC_I_CONTINUE_NEEDED = 0x00090312
|
|
166
|
+
|
|
167
|
+
# These are generally returned by InitializeSecurityContext
|
|
168
|
+
SEC_E_INSUFFICIENT_MEMORY = 0x80090300
|
|
169
|
+
SEC_E_INTERNAL_ERROR = 0x80090304
|
|
170
|
+
SEC_E_INVALID_HANDLE = 0x80090301
|
|
171
|
+
SEC_E_INVALID_TOKEN = 0x80090308
|
|
172
|
+
SEC_E_LOGON_DENIED = 0x8009030C
|
|
173
|
+
SEC_E_NO_AUTHENTICATING_AUTHORITY = 0x80090311
|
|
174
|
+
SEC_E_NO_CREDENTIALS = 0x8009030E
|
|
175
|
+
SEC_E_TARGET_UNKNOWN = 0x80090303
|
|
176
|
+
SEC_E_UNSUPPORTED_FUNCTION = 0x80090302
|
|
177
|
+
SEC_E_WRONG_PRINCIPAL = 0x80090322
|
|
178
|
+
|
|
179
|
+
# These are generally returned by AcquireCredentialsHandle
|
|
180
|
+
SEC_E_NOT_OWNER = 0x80090306
|
|
181
|
+
SEC_E_SECPKG_NOT_FOUND = 0x80090305
|
|
182
|
+
SEC_E_UNKNOWN_CREDENTIALS = 0x8009030D
|
|
183
|
+
|
|
184
|
+
RESULT_MAP = constants.to_h {|v| [const_get(v), v]}.freeze
|
|
185
|
+
|
|
186
|
+
attr_reader :value
|
|
187
|
+
|
|
188
|
+
def initialize(value)
|
|
189
|
+
# convert to unsigned long
|
|
190
|
+
value &= 0xffffffff
|
|
191
|
+
raise "#{value.to_s(16)} is not a recognized result" unless RESULT_MAP.key? value
|
|
192
|
+
@value = value
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def to_s
|
|
196
|
+
RESULT_MAP[@value].to_s
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def ok?
|
|
200
|
+
@value == SEC_I_CONTINUE_NEEDED || @value == SEC_E_OK
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def ==(other)
|
|
204
|
+
case other
|
|
205
|
+
when SSPIResult
|
|
206
|
+
@value == other.value
|
|
207
|
+
when Integer
|
|
208
|
+
@value == other
|
|
209
|
+
when Symbol
|
|
210
|
+
RESULT_MAP[@value] == other
|
|
211
|
+
else
|
|
212
|
+
false
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Handles "Negotiate" type authentication. Geared towards authenticating with a proxy server over HTTP
|
|
218
|
+
class NegotiateAuth
|
|
219
|
+
attr_accessor :credentials, :context, :contextAttributes, :user, :domain
|
|
220
|
+
|
|
221
|
+
# Default request flags for SSPI functions
|
|
222
|
+
REQUEST_FLAGS = ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONNECTION
|
|
223
|
+
|
|
224
|
+
# NTLM tokens start with this header always. Encoding alone adds "==" and newline, so remove those
|
|
225
|
+
B64_TOKEN_PREFIX = ["NTLMSSP"].pack("m").delete("=\n")
|
|
226
|
+
|
|
227
|
+
# Given a connection and a request path, performs authentication as the current user and returns
|
|
228
|
+
# the response from a GET request. The connection should be a Net::HTTP object, and it should
|
|
229
|
+
# have been constructed using the Net::HTTP.Proxy method, but anything that responds to "get" will work.
|
|
230
|
+
# If a user and domain are given, will authenticate as the given user.
|
|
231
|
+
# Returns the response received from the get method (usually Net::HTTPResponse)
|
|
232
|
+
def NegotiateAuth.proxy_auth_get(http, path, user = nil, domain = nil)
|
|
233
|
+
raise "http must respond to :get" unless http.respond_to?(:get)
|
|
234
|
+
nego_auth = self.new user, domain
|
|
235
|
+
|
|
236
|
+
resp = http.get path, { "Proxy-Authorization" => "Negotiate " + nego_auth.get_initial_token }
|
|
237
|
+
if resp["Proxy-Authenticate"]
|
|
238
|
+
resp = http.get path, { "Proxy-Authorization" => "Negotiate " + nego_auth.complete_authentication(resp["Proxy-Authenticate"].split(" ").last.strip) }
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
resp
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Creates a new instance ready for authentication as the given user in the given domain.
|
|
245
|
+
# Defaults to current user and domain as defined by ENV["USERDOMAIN"] and ENV["USERNAME"] if
|
|
246
|
+
# no arguments are supplied.
|
|
247
|
+
def initialize(user = nil, domain = nil)
|
|
248
|
+
if user.nil? && domain.nil? && ENV["USERNAME"].nil? && ENV["USERDOMAIN"].nil?
|
|
249
|
+
raise "A username or domain must be supplied since they cannot be retrieved from the environment"
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
@user = user || ENV["USERNAME"]
|
|
253
|
+
@domain = domain || ENV["USERDOMAIN"]
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Gets the initial Negotiate token. Returns it as a base64 encoded string suitable for use in HTTP. Can
|
|
257
|
+
# be easily decoded, however.
|
|
258
|
+
def get_initial_token
|
|
259
|
+
raise "This object is no longer usable because its resources have been freed." if @cleaned_up
|
|
260
|
+
get_credentials
|
|
261
|
+
|
|
262
|
+
outputBuffer = SecurityBuffer.new
|
|
263
|
+
@context = CtxtHandle.new
|
|
264
|
+
@contextAttributes = "\0" * 4
|
|
265
|
+
|
|
266
|
+
result = SSPIResult.new(API::InitializeSecurityContextA.call(@credentials.to_p, nil, nil,
|
|
267
|
+
REQUEST_FLAGS,0, SECURITY_NETWORK_DREP, nil, 0, @context.to_p, outputBuffer.to_p, @contextAttributes, TimeStamp.new.to_p))
|
|
268
|
+
|
|
269
|
+
if result.ok? then
|
|
270
|
+
return encode_token(outputBuffer.token)
|
|
271
|
+
else
|
|
272
|
+
raise "Error: #{result.to_s}"
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Takes a token and gets the next token in the Negotiate authentication chain. Token can be Base64 encoded or not.
|
|
277
|
+
# The token can include the "Negotiate" header and it will be stripped.
|
|
278
|
+
# Does not indicate if SEC_I_CONTINUE or SEC_E_OK was returned.
|
|
279
|
+
# Token returned is Base64 encoded w/ all new lines removed.
|
|
280
|
+
def complete_authentication(token)
|
|
281
|
+
raise "This object is no longer usable because its resources have been freed." if @cleaned_up
|
|
282
|
+
|
|
283
|
+
# Nil token OK, just set it to empty string
|
|
284
|
+
token = "" if token.nil?
|
|
285
|
+
|
|
286
|
+
if token.start_with? "Negotiate"
|
|
287
|
+
# If the Negotiate prefix is passed in, assume we are seeing "Negotiate <token>" and get the token.
|
|
288
|
+
token = token.split(" ", 2).last
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
if token.start_with? B64_TOKEN_PREFIX
|
|
292
|
+
# indicates base64 encoded token
|
|
293
|
+
token = token.strip.unpack1("m")
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
outputBuffer = SecurityBuffer.new
|
|
297
|
+
result = SSPIResult.new(API::InitializeSecurityContextA.call(@credentials.to_p, @context.to_p, nil,
|
|
298
|
+
REQUEST_FLAGS, 0, SECURITY_NETWORK_DREP, SecurityBuffer.new(token).to_p, 0,
|
|
299
|
+
@context.to_p,
|
|
300
|
+
outputBuffer.to_p, @contextAttributes, TimeStamp.new.to_p))
|
|
301
|
+
|
|
302
|
+
if result.ok? then
|
|
303
|
+
return encode_token(outputBuffer.token)
|
|
304
|
+
else
|
|
305
|
+
raise "Error: #{result.to_s}"
|
|
306
|
+
end
|
|
307
|
+
ensure
|
|
308
|
+
# need to make sure we don't clean up if we've already cleaned up.
|
|
309
|
+
clean_up unless @cleaned_up
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
private
|
|
313
|
+
|
|
314
|
+
def clean_up
|
|
315
|
+
# free structures allocated
|
|
316
|
+
@cleaned_up = true
|
|
317
|
+
API::FreeCredentialsHandle.call(@credentials.to_p)
|
|
318
|
+
API::DeleteSecurityContext.call(@context.to_p)
|
|
319
|
+
@context = nil
|
|
320
|
+
@credentials = nil
|
|
321
|
+
@contextAttributes = nil
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# Gets credentials based on user, domain or both. If both are nil, an error occurs
|
|
325
|
+
def get_credentials
|
|
326
|
+
@credentials = CredHandle.new
|
|
327
|
+
ts = TimeStamp.new
|
|
328
|
+
@identity = Identity.new @user, @domain
|
|
329
|
+
result = SSPIResult.new(API::AcquireCredentialsHandleA.call(nil, "Negotiate", SECPKG_CRED_OUTBOUND, nil, @identity.to_p,
|
|
330
|
+
nil, nil, @credentials.to_p, ts.to_p))
|
|
331
|
+
raise "Error acquire credentials: #{result}" unless result.ok?
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def encode_token(t)
|
|
335
|
+
[t].pack("m0")
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
end
|
data/rakelib/epoch.rake
ADDED
data/sig/win32/sspi.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: net-http-sspi
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Justin Bailey
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2024-10-09 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: fiddle
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '1.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '1.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: net-http
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
description: implements bindings to Win32 SSPI functions, focused on authentication
|
|
41
|
+
to a proxy server over HTTP.
|
|
42
|
+
email:
|
|
43
|
+
- "<jgbailey@gmail.com>"
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".mailmap"
|
|
49
|
+
- BSDL
|
|
50
|
+
- COPYING
|
|
51
|
+
- README.md
|
|
52
|
+
- lib/win32/sspi.rb
|
|
53
|
+
- rakelib/epoch.rake
|
|
54
|
+
- sig/win32/sspi.rbs
|
|
55
|
+
homepage: https://github.com/ruby/net-http-sspi
|
|
56
|
+
licenses:
|
|
57
|
+
- Ruby
|
|
58
|
+
- BSDL
|
|
59
|
+
metadata:
|
|
60
|
+
homepage_uri: https://github.com/ruby/net-http-sspi
|
|
61
|
+
source_code_uri: https://github.com/ruby/net-http-sspi
|
|
62
|
+
rdoc_options: []
|
|
63
|
+
require_paths:
|
|
64
|
+
- lib
|
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 3.0.0
|
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
requirements: []
|
|
76
|
+
rubygems_version: 3.6.0.dev
|
|
77
|
+
specification_version: 4
|
|
78
|
+
summary: Windows SSPI implementation in Ruby
|
|
79
|
+
test_files: []
|