|
| 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 | + # |
| 14 | + class ExternalAuthenticator |
| 15 | + |
| 16 | + # :call-seq: |
| 17 | + # initial_response? -> true |
| 18 | + # |
| 19 | + # +EXTERNAL+ can send an initial client response. |
| 20 | + def initial_response?; true end |
| 21 | + |
| 22 | + ## |
| 23 | + # :call-seq: |
| 24 | + # new -> authenticator |
| 25 | + # new(authzid, **) -> authenticator |
| 26 | + # new(authzid:, **) -> authenticator |
| 27 | + # |
| 28 | + # Creates an Authenticator for the "+EXTERNAL+" SASL mechanism, as |
| 29 | + # specified in RFC-4422[https://tools.ietf.org/html/rfc4422]. To use |
| 30 | + # this, see Net::IMAP#authenticate or your client's authentication |
| 31 | + # method. |
| 32 | + # |
| 33 | + # ==== Configuration parameters |
| 34 | + # Only one parameter, which is optional: |
| 35 | + # |
| 36 | + # * #authzid -- the identity to act as. Leave blank to use the identity |
| 37 | + # associated with the client's credentials. |
| 38 | + # |
| 39 | + # May be sent as a positional argument or as a keyword argument. |
| 40 | + # |
| 41 | + def initialize(authzid_arg = nil, authzid: nil) |
| 42 | + @authzid = authzid || authzid_arg |
| 43 | + [authzid, authzid_arg].compact.count <= 1 or |
| 44 | + raise ArgumentError, "conflicting values for authzid" |
| 45 | + if @authzid |
| 46 | + raise ArgumentError, "contains NULL" if @authzid.include? NULL |
| 47 | + @authzid = @authzid.encode "UTF-8" |
| 48 | + @authzid.valid_encoding? or raise ArgumentError, "not valid UTF-8" |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + attr_reader :authzid |
| 53 | + |
| 54 | + def process(_) |
| 55 | + return "" if authzid.nil? |
| 56 | + if /\u0000/u.match?(authzid) # also validates UTF8 encoding |
| 57 | + raise DataFormatError, "authzid contains NULL" |
| 58 | + end |
| 59 | + authzid.encode "UTF-8" |
| 60 | + end |
| 61 | + |
| 62 | + def authzid=(value) |
| 63 | + raise ArgumentError, "contains NULL" if value.include? NULL |
| 64 | + value = value.encode "UTF-8" |
| 65 | + value.valid_encoding? or raise ArgumentError, "not valid UTF-8" |
| 66 | + super(value) |
| 67 | + end |
| 68 | + |
| 69 | + private |
| 70 | + |
| 71 | + NULL = "\0" |
| 72 | + private_constant :NULL |
| 73 | + |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments