|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Net |
| 4 | + class IMAP < Protocol |
| 5 | + module SASL |
| 6 | + |
| 7 | + # Authenticator for the "+EXTERNAL+" SASL mechanism, as specified by |
| 8 | + # RFC-4422[https://tools.ietf.org/html/rfc4422]. See |
| 9 | + # Net::IMAP#authenticate. |
| 10 | + # |
| 11 | + # The EXTERNAL mechanism requests that the server use client credentials |
| 12 | + # established external to SASL, for example by TLS certificate or IPsec. |
| 13 | + class ExternalAuthenticator |
| 14 | + |
| 15 | + # :call-seq: |
| 16 | + # new(authzid: nil, **) -> authenticator |
| 17 | + # |
| 18 | + # Creates an Authenticator for the "+EXTERNAL+" SASL mechanism, as |
| 19 | + # specified in RFC-4422[https://tools.ietf.org/html/rfc4422]. To use |
| 20 | + # this, see Net::IMAP#authenticate or your client's authentication |
| 21 | + # method. |
| 22 | + # |
| 23 | + # #authzid is an optional identity to act as or on behalf of. |
| 24 | + # |
| 25 | + # Any other keyword parameters are quietly ignored. |
| 26 | + def initialize(authzid: nil) |
| 27 | + @authzid = authzid&.to_str&.encode "UTF-8" |
| 28 | + if @authzid&.match?(/\u0000/u) # also validates UTF8 encoding |
| 29 | + raise ArgumentError, "contains NULL" |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # Authorization identity: an identity to act as or on behalf of. |
| 34 | + # |
| 35 | + # If not explicitly provided, the server defaults to using the identity |
| 36 | + # that was authenticated by the external credentials. |
| 37 | + attr_reader :authzid |
| 38 | + |
| 39 | + # :call-seq: |
| 40 | + # initial_response? -> true |
| 41 | + # |
| 42 | + # +EXTERNAL+ can send an initial client response. |
| 43 | + def initial_response?; true end |
| 44 | + |
| 45 | + # Returns #authzid, or an empty string if there is no authzid. |
| 46 | + def process(_) |
| 47 | + authzid || "" |
| 48 | + end |
| 49 | + |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | +end |
0 commit comments