|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "authenticator" |
| 4 | +require_relative "gs2_header" |
| 5 | + |
| 6 | +module Net |
| 7 | + class IMAP < Protocol |
| 8 | + module SASL |
| 9 | + |
| 10 | + # Abstract base class for the SASL mechanisms defined in |
| 11 | + # RFC7628[https://tools.ietf.org/html/rfc7628]: |
| 12 | + # * OAUTHBEARER[rdoc-ref:OAuthBearerAuthenticator] |
| 13 | + # * OAUTH10A |
| 14 | + class OAuthAuthenticator < Authenticator |
| 15 | + include GS2Header |
| 16 | + |
| 17 | + # Creates an OAuthBearerAuthenticator or OAuth10aAuthenticator. |
| 18 | + # |
| 19 | + # * +_subclass_var_+ — the subclass's required parameter. |
| 20 | + # * #authzid ― Identity to act as or on behalf of. |
| 21 | + # * #host — Hostname to which the client connected. |
| 22 | + # * #port — Service port to which the client connected. |
| 23 | + # * #mthd — HTTP method |
| 24 | + # * #path — HTTP path data |
| 25 | + # * #post — HTTP post data |
| 26 | + # * #qs — HTTP query string |
| 27 | + # |
| 28 | + # All properties here are optional. See the child classes for their |
| 29 | + # required parameter(s). |
| 30 | + # |
| 31 | + def initialize(arg1_authzid = nil, _ = nil, arg3_authzid = nil, |
| 32 | + authzid: nil, host: nil, port: nil, |
| 33 | + mthd: nil, path: nil, post: nil, qs: nil, **) |
| 34 | + super |
| 35 | + propinit(:authzid, authzid, arg1_authzid, arg3_authzid) |
| 36 | + self.host = host |
| 37 | + self.port = port |
| 38 | + self.mthd = mthd |
| 39 | + self.path = path |
| 40 | + self.post = post |
| 41 | + self.qs = qs |
| 42 | + @done = false |
| 43 | + end |
| 44 | + |
| 45 | + ## |
| 46 | + # Authorization identity: an identity to act as or on behalf of. |
| 47 | + # |
| 48 | + # For the OAuth-based mechanisms, authcid is implicitly set by the |
| 49 | + # #auth_payload. It may be useful to make it explicit, which allows the |
| 50 | + # server to verify the credentials match the identity. The gs2_header |
| 51 | + # MAY include the username associated with the resource being accessed, |
| 52 | + # the "authzid". |
| 53 | + # |
| 54 | + # It is worth noting that application protocols are allowed to require |
| 55 | + # an authzid, as are specific server implementations. |
| 56 | + # |
| 57 | + # See also: PlainAuthenticator#authzid, DigestMD5Authenticator#authzid. |
| 58 | + property :authzid |
| 59 | + |
| 60 | + ## |
| 61 | + # Hostname to which the client connected. |
| 62 | + property :host |
| 63 | + |
| 64 | + ## |
| 65 | + # Service port to which the client connected. |
| 66 | + property :port |
| 67 | + |
| 68 | + ## |
| 69 | + # HTTP method. (optional) |
| 70 | + property :mthd |
| 71 | + |
| 72 | + ## |
| 73 | + # HTTP path data. (optional) |
| 74 | + property :path |
| 75 | + |
| 76 | + ## |
| 77 | + # HTTP post data. (optional) |
| 78 | + property :post |
| 79 | + |
| 80 | + ## |
| 81 | + # The query string. (optional) |
| 82 | + property :qs |
| 83 | + |
| 84 | + # Stores the most recent server "challenge". When authentication fails, |
| 85 | + # this may hold information about the failure reason, as JSON. |
| 86 | + attr_reader :last_server_response |
| 87 | + |
| 88 | + ## |
| 89 | + # Returns initial_client_response the first time, then "<tt>^A</tt>". |
| 90 | + def process(data) |
| 91 | + @last_server_response = data |
| 92 | + return "\1" if done? |
| 93 | + initial_client_response |
| 94 | + ensure |
| 95 | + @done = true |
| 96 | + end |
| 97 | + |
| 98 | + ## |
| 99 | + # Returns true when the initial client response was sent. |
| 100 | + # |
| 101 | + # The authentication should not succeed unless this returns true, but it |
| 102 | + # does *not* indicate success. |
| 103 | + def done?; @done end |
| 104 | + |
| 105 | + # The {RFC7628 §3.1}[https://www.rfc-editor.org/rfc/rfc7628#section-3.1] |
| 106 | + # formatted response. |
| 107 | + def initial_client_response |
| 108 | + [gs2_header, *kv_pairs.map {|kv| kv.join("=") }, "\1"].join("\1") |
| 109 | + end |
| 110 | + |
| 111 | + # The key value pairs which follow gs2_header, as a Hash. |
| 112 | + def kv_pairs |
| 113 | + { |
| 114 | + host: host, port: port, mthd: mthd, path: path, post: post, qs: qs, |
| 115 | + auth: auth_payload, # auth_payload is implemented by subclasses |
| 116 | + }.compact |
| 117 | + end |
| 118 | + |
| 119 | + # What would be sent in the HTTP Authorization header. |
| 120 | + # |
| 121 | + # <b>Implemented by subclasses.</b> |
| 122 | + def auth_payload; raise NotImplementedError, "implement in subclass" end |
| 123 | + |
| 124 | + end |
| 125 | + |
| 126 | + # Authenticator for the "+OAUTHBEARER+" SASL mechanism, specified in |
| 127 | + # RFC7628[https://tools.ietf.org/html/rfc7628]. Use via |
| 128 | + # Net::IMAP#authenticate. |
| 129 | + # |
| 130 | + # TODO... |
| 131 | + # |
| 132 | + # OAuth 2.0 bearer tokens, as described in [RFC6750]. |
| 133 | + # RFC6750 uses Transport Layer Security (TLS) [RFC5246] to |
| 134 | + # secure the protocol interaction between the client and the |
| 135 | + # resource server. |
| 136 | + # |
| 137 | + # TLS MUST be used for +OAUTHBEARER+ to protect the bearer token. |
| 138 | + class OAuthBearerAuthenticator < OAuthAuthenticator |
| 139 | + |
| 140 | + ## |
| 141 | + # :call-seq: |
| 142 | + # new(authzid, oauth2_token, **) -> auth_ctx |
| 143 | + # new(oauth2_token:, authzid: nil, **) -> auth_ctx |
| 144 | + # new(**) {|propname, auth_ctx| propval } -> auth_ctx |
| 145 | + # |
| 146 | + # Creates an Authenticator for the "+OAUTHBEARER+" SASL mechanism. |
| 147 | + # |
| 148 | + # Called by Net::IMAP#authenticate and similar methods on other clients. |
| 149 | + # |
| 150 | + # === Properties |
| 151 | + # |
| 152 | + # * #oauth2_token — An OAuth2 bearer token or access token. *Required* |
| 153 | + # * #authzid ― Identity to act as or on behalf of. |
| 154 | + # * #host — Hostname to which the client connected. |
| 155 | + # * #port — Service port to which the client connected. |
| 156 | + # * See other, rarely used properties on OAuthAuthenticator. |
| 157 | + # |
| 158 | + # Although only #oauth2_token is required, specific server |
| 159 | + # implementations may additionally require #authzid, #host, and #port. |
| 160 | + # |
| 161 | + # See the documentation on each property method for more details. |
| 162 | + # |
| 163 | + # All three properties may be sent as either positional or keyword |
| 164 | + # arguments. See Net::IMAP::SASL::Authenticator@Properties for a |
| 165 | + # detailed description of property assignment, lazy loading, and |
| 166 | + # callbacks. |
| 167 | + # |
| 168 | + def initialize(id1=nil, arg2_token=nil, id3=nil, oauth2_token: nil, **) |
| 169 | + super # handles authzid, host, port, callback, etc |
| 170 | + propinit(:oauth2_token, oauth2_token, arg2_token, required: true) |
| 171 | + self.host = host |
| 172 | + end |
| 173 | + |
| 174 | + ## |
| 175 | + # An OAuth2 bearer token, which is generally the same as the standard |
| 176 | + # access_token. |
| 177 | + property :oauth2_token |
| 178 | + |
| 179 | + # :call-seq: |
| 180 | + # initial_response? -> true |
| 181 | + # |
| 182 | + # +OAUTHBEARER+ sends an initial client response. |
| 183 | + def initial_response?; true end |
| 184 | + |
| 185 | + # What would be sent in the HTTP Authorization header. |
| 186 | + def auth_payload; "Bearer #{oauth2_token}" end |
| 187 | + |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + end |
| 192 | +end |
0 commit comments