@@ -14,18 +14,25 @@ module SASL
1414 class OAuthAuthenticator
1515 include GS2Header
1616
17- # Authorization identity: an identity to act as or on behalf of.
17+ # Authorization identity: an identity to act as or on behalf of. The
18+ # identity form is application protocol specific. If not provided or
19+ # left blank, the server derives an authorization identity from the
20+ # authentication identity. The server is responsible for verifying the
21+ # client's credentials and verifying that the identity it associates
22+ # with the client's authentication identity is allowed to act as (or on
23+ # behalf of) the authorization identity.
24+ #
25+ # For example, an administrator or superuser might take on another role:
26+ #
27+ # imap.authenticate "PLAIN", "root", passwd, authzid: "user"
1828 #
19- # If no explicit authorization identity is provided, it is usually
20- # derived from the authentication identity. For the OAuth-based
21- # mechanisms, the authentication identity is the identity established by
22- # the OAuth credential.
2329 attr_reader :authzid
30+ alias username authzid
2431
25- # Hostname to which the client connected.
32+ # Hostname to which the client connected. (optional)
2633 attr_reader :host
2734
28- # Service port to which the client connected.
35+ # Service port to which the client connected. (optional)
2936 attr_reader :port
3037
3138 # HTTP method. (optional)
@@ -39,6 +46,7 @@ class OAuthAuthenticator
3946
4047 # The query string. (optional)
4148 attr_reader :qs
49+ alias query qs
4250
4351 # Stores the most recent server "challenge". When authentication fails,
4452 # this may hold information about the failure reason, as JSON.
@@ -47,29 +55,42 @@ class OAuthAuthenticator
4755 # Creates an RFC7628[https://tools.ietf.org/html/rfc7628] OAuth
4856 # authenticator.
4957 #
50- # === Options
58+ # ==== Parameters
59+ #
60+ # See child classes for required parameter(s). The following parameters
61+ # are all optional, but it is worth noting that <b>application protocols
62+ # are allowed to require</b> #authzid (or other parameters, such as
63+ # #host or #port) <b>as are specific server implementations</b>.
64+ #
65+ # * _optional_ #authzid ― Authorization identity to act as or on behalf of.
66+ #
67+ # _optional_ #username — An alias for #authzid.
5168 #
52- # See child classes for required configuration parameter(s). The
53- # following parameters are all optional, but protocols or servers may
54- # add requirements for #authzid, #host, #port, or any other parameter.
69+ # Note that, unlike some other authenticators, +username+ sets the
70+ # _authorization_ identity and not the _authentication_ identity. The
71+ # authentication identity is established for the client by the OAuth
72+ # token.
5573 #
56- # * #authzid ― Identity to act as or on behalf of.
57- # * #host — Hostname to which the client connected.
58- # * #port — Service port to which the client connected.
59- # * #mthd — HTTP method
60- # * #path — HTTP path data
61- # * #post — HTTP post data
62- # * #qs — HTTP query string
74+ # * _optional_ #host — Hostname to which the client connected.
75+ # * _optional_ #port — Service port to which the client connected.
76+ # * _optional_ #mthd — HTTP method
77+ # * _optional_ #path — HTTP path data
78+ # * _optional_ #post — HTTP post data
79+ # * _optional_ #qs — HTTP query string
6380 #
81+ # _optional_ #query — An alias for #qs
82+ #
83+ # Any other keyword parameters are quietly ignored.
6484 def initialize ( authzid : nil , host : nil , port : nil ,
85+ username : nil , query : nil ,
6586 mthd : nil , path : nil , post : nil , qs : nil , **)
66- @authzid = authzid
87+ @authzid = authzid || username
6788 @host = host
6889 @port = port
6990 @mthd = mthd
7091 @path = path
7192 @post = post
72- @qs = qs
93+ @qs = qs || query
7394 @done = false
7495 end
7596
@@ -116,34 +137,45 @@ def authorization; raise "must be implemented by subclass" end
116137 # the bearer token.
117138 class OAuthBearerAuthenticator < OAuthAuthenticator
118139
119- # An OAuth2 bearer token, generally the access token.
140+ # An OAuth 2.0 bearer token. See {RFC-6750}[https://www.rfc-editor.org/rfc/rfc6750]
120141 attr_reader :oauth2_token
121142
122143 # :call-seq:
123- # new(oauth2_token, **options) -> authenticator
124- # new(oauth2_token:, **options) -> authenticator
144+ # new(oauth2_token, **options) -> authenticator
145+ # new(authzid, oauth2_token, **options) -> authenticator
146+ # new(oauth2_token:, **options) -> authenticator
125147 #
126148 # Creates an Authenticator for the "+OAUTHBEARER+" SASL mechanism.
127149 #
128150 # Called by Net::IMAP#authenticate and similar methods on other clients.
129151 #
130- # === Options
152+ # ==== Parameters
153+ #
154+ # * #oauth2_token — An OAuth2 bearer token
155+ #
156+ # All other keyword parameters are passed to
157+ # {super}[rdoc-ref:OAuthAuthenticator::new] (see OAuthAuthenticator).
158+ # The most common ones are:
159+ #
160+ # * _optional_ #authzid ― Authorization identity to act as or on behalf of.
161+ #
162+ # _optional_ #username — An alias for #authzid.
131163 #
132- # Only +oauth2_token+ is required by the mechanism, however protocols
133- # and servers may add requirements for #authzid, #host, #port, or any
134- # other parameter.
164+ # Note that, unlike some other authenticators, +username+ sets the
165+ # _authorization_ identity and not the _authentication_ identity. The
166+ # authentication identity is established for the client by
167+ # #oauth2_token.
135168 #
136- # * #oauth2_token — An OAuth2 bearer token or access token. *Required.*
137- # May be provided as either regular or keyword argument.
138- # * #authzid ― Identity to act as or on behalf of.
139- # * #host — Hostname to which the client connected.
140- # * #port — Service port to which the client connected.
141- # * See OAuthAuthenticator documentation for less common parameters.
169+ # * _optional_ #host — Hostname to which the client connected.
170+ # * _optional_ #port — Service port to which the client connected.
142171 #
143- def initialize ( oauth2_token_arg = nil , oauth2_token : nil , **args , &blk )
144- super ( **args , &blk ) # handles authzid, host, port, etc
145- oauth2_token && oauth2_token_arg and
146- raise ArgumentError , "conflicting values for oauth2_token"
172+ # Although only oauth2_token is required by this mechanism, it is worth
173+ # noting that <b><em>application protocols are allowed to
174+ # require</em></b> #authzid (<em>or other parameters, such as</em> #host
175+ # _or_ #port) <b><em>as are specific server implementations</em></b>.
176+ def initialize ( arg1 = nil , arg2 = nil , oauth2_token : nil , **args , &blk )
177+ username , oauth2_token_arg = arg2 . nil? ? [ nil , arg1 ] : [ arg1 , arg2 ]
178+ super ( username : username , **args , &blk )
147179 @oauth2_token = oauth2_token || oauth2_token_arg or
148180 raise ArgumentError , "missing oauth2_token"
149181 end
0 commit comments