|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "gs2_header" |
| 4 | + |
| 5 | +module Net |
| 6 | + class IMAP < Protocol |
| 7 | + module SASL |
| 8 | + |
| 9 | + # Abstract base class for the SASL mechanisms defined in |
| 10 | + # RFC7628[https://tools.ietf.org/html/rfc7628]: |
| 11 | + # * OAUTHBEARER[rdoc-ref:OAuthBearerAuthenticator] |
| 12 | + # (OAuthBearerAuthenticator) |
| 13 | + # * OAUTH10A |
| 14 | + class OAuthAuthenticator |
| 15 | + include GS2Header |
| 16 | + |
| 17 | + # Creates an RFC7628[https://tools.ietf.org/html/rfc7628] OAuth |
| 18 | + # authenticator. |
| 19 | + # |
| 20 | + # === Configuration parameters |
| 21 | + # |
| 22 | + # See child classes for required configuration parameter(s). The |
| 23 | + # following parameters are all optional, but protocols or servers may |
| 24 | + # add requirements for #authzid, #host, #port, or any other parameter. |
| 25 | + # |
| 26 | + # * #authzid ― Identity to act as or on behalf of. |
| 27 | + # * #host — Hostname to which the client connected. |
| 28 | + # * #port — Service port to which the client connected. |
| 29 | + # * #mthd — HTTP method |
| 30 | + # * #path — HTTP path data |
| 31 | + # * #post — HTTP post data |
| 32 | + # * #qs — HTTP query string |
| 33 | + # |
| 34 | + def initialize(authzid: nil, host: nil, port: nil, |
| 35 | + mthd: nil, path: nil, post: nil, qs: nil, **) |
| 36 | + @authzid = authzid |
| 37 | + @host = host |
| 38 | + @port = port |
| 39 | + @mthd = mthd |
| 40 | + @path = path |
| 41 | + @post = post |
| 42 | + @qs = qs |
| 43 | + @done = false |
| 44 | + end |
| 45 | + |
| 46 | + # Authorization identity: an identity to act as or on behalf of. |
| 47 | + # |
| 48 | + # If no explicit authorization identity is provided, it is usually |
| 49 | + # derived from the authentication identity. For the OAuth-based |
| 50 | + # mechanisms, the authentication identity is the identity established by |
| 51 | + # the OAuth credential. |
| 52 | + # |
| 53 | + # See also: PlainAuthenticator#authzid, DigestMD5Authenticator#authzid. |
| 54 | + attr_reader :authzid |
| 55 | + |
| 56 | + # Hostname to which the client connected. |
| 57 | + attr_reader :host |
| 58 | + |
| 59 | + # Service port to which the client connected. |
| 60 | + attr_reader :port |
| 61 | + |
| 62 | + # HTTP method. (optional) |
| 63 | + attr_reader :mthd |
| 64 | + |
| 65 | + # HTTP path data. (optional) |
| 66 | + attr_reader :path |
| 67 | + |
| 68 | + # HTTP post data. (optional) |
| 69 | + attr_reader :post |
| 70 | + |
| 71 | + # The query string. (optional) |
| 72 | + attr_reader :qs |
| 73 | + |
| 74 | + # Stores the most recent server "challenge". When authentication fails, |
| 75 | + # this may hold information about the failure reason, as JSON. |
| 76 | + attr_reader :last_server_response |
| 77 | + |
| 78 | + # Returns initial_client_response the first time, then "<tt>^A</tt>". |
| 79 | + def process(data) |
| 80 | + @last_server_response = data |
| 81 | + return "\1" if done? |
| 82 | + initial_client_response |
| 83 | + ensure |
| 84 | + @done = true |
| 85 | + end |
| 86 | + |
| 87 | + # Returns true when the initial client response was sent. |
| 88 | + # |
| 89 | + # The authentication should not succeed unless this returns true, but it |
| 90 | + # does *not* indicate success. |
| 91 | + def done?; @done end |
| 92 | + |
| 93 | + # The {RFC7628 §3.1}[https://www.rfc-editor.org/rfc/rfc7628#section-3.1] |
| 94 | + # formatted response. |
| 95 | + def initial_client_response |
| 96 | + kv_pairs = { |
| 97 | + host: host, port: port, mthd: mthd, path: path, post: post, qs: qs, |
| 98 | + auth: authorization, # authorization is implemented by subclasses |
| 99 | + }.compact |
| 100 | + [gs2_header, *kv_pairs.map {|kv| kv.join("=") }, "\1"].join("\1") |
| 101 | + end |
| 102 | + |
| 103 | + # Value of the HTTP Authorization header |
| 104 | + # |
| 105 | + # <b>Implemented by subclasses.</b> |
| 106 | + def authorization; raise "must be implemented by subclass" end |
| 107 | + |
| 108 | + end |
| 109 | + |
| 110 | + # Authenticator for the "+OAUTHBEARER+" SASL mechanism, specified in |
| 111 | + # RFC7628[https://tools.ietf.org/html/rfc7628]. Authenticates using OAuth |
| 112 | + # 2.0 bearer tokens, as described in |
| 113 | + # RFC6750[https://tools.ietf.org/html/rfc6750]. Use via |
| 114 | + # Net::IMAP#authenticate. |
| 115 | + # |
| 116 | + # RFC6750[https://tools.ietf.org/html/rfc6750] requires Transport Layer |
| 117 | + # Security (TLS) [RFC5246] to secure the protocol interaction between the |
| 118 | + # client and the resource server. TLS _MUST_ be used for +OAUTHBEARER+ to |
| 119 | + # protect the bearer token. |
| 120 | + class OAuthBearerAuthenticator < OAuthAuthenticator |
| 121 | + |
| 122 | + ## |
| 123 | + # :call-seq: |
| 124 | + # new(oauth2_token, **) -> auth_ctx |
| 125 | + # new(oauth2_token:, **) -> auth_ctx |
| 126 | + # |
| 127 | + # Creates an Authenticator for the "+OAUTHBEARER+" SASL mechanism. |
| 128 | + # |
| 129 | + # Called by Net::IMAP#authenticate and similar methods on other clients. |
| 130 | + # |
| 131 | + # === Parameters |
| 132 | + # |
| 133 | + # Only +oauth2_token+ is required by the mechanism, however protocols |
| 134 | + # and servers may add requirements for #authzid, #host, #port, or any |
| 135 | + # other parameter. |
| 136 | + # |
| 137 | + # * #oauth2_token — An OAuth2 bearer token or access token. *Required* |
| 138 | + # May be provided as either regular or keyword argument. |
| 139 | + # * #authzid ― Identity to act as or on behalf of. |
| 140 | + # * #host — Hostname to which the client connected. |
| 141 | + # * #port — Service port to which the client connected. |
| 142 | + # * See OAuthAuthenticator documentation for less common parameters. |
| 143 | + # |
| 144 | + def initialize(oauth2_token_arg = nil, oauth2_token: nil, **args, &blk) |
| 145 | + super(**args, &blk) # handles authzid, host, port, etc |
| 146 | + oauth2_token && oauth2_token_arg and |
| 147 | + raise ArgumentError, "conflicting values for oauth2_token" |
| 148 | + @oauth2_token = oauth2_token || oauth2_token_arg or |
| 149 | + raise ArgumentError, "missing oauth2_token" |
| 150 | + end |
| 151 | + |
| 152 | + # An OAuth2 bearer token, generally the access token. |
| 153 | + attr_reader :oauth2_token |
| 154 | + |
| 155 | + # :call-seq: |
| 156 | + # initial_response? -> true |
| 157 | + # |
| 158 | + # +OAUTHBEARER+ sends an initial client response. |
| 159 | + def initial_response?; true end |
| 160 | + |
| 161 | + # Value of the HTTP Authorization header |
| 162 | + def authorization; "Bearer #{oauth2_token}" end |
| 163 | + |
| 164 | + end |
| 165 | + end |
| 166 | + |
| 167 | + end |
| 168 | +end |
0 commit comments