CreateSASL creates the sasl.Server instance for the corresponding mechanism.
( mech string, remoteAddr net.Addr, successCb func(identity string, data ContextData) error, )
| 135 | |
| 136 | // CreateSASL creates the sasl.Server instance for the corresponding mechanism. |
| 137 | func (s *SASLAuth) CreateSASL( |
| 138 | mech string, remoteAddr net.Addr, |
| 139 | successCb func(identity string, data ContextData) error, |
| 140 | ) sasl.Server { |
| 141 | switch mech { |
| 142 | case sasl.Plain: |
| 143 | return sasl.NewPlainServer(func(identity, username, password string) error { |
| 144 | if identity == "" { |
| 145 | identity = username |
| 146 | } |
| 147 | if identity != username { |
| 148 | if s.ErrorMap != nil { |
| 149 | return s.ErrorMap(ErrInvalidAuthCred) |
| 150 | } |
| 151 | return ErrInvalidAuthCred |
| 152 | } |
| 153 | |
| 154 | err := s.AuthPlain(username, password) |
| 155 | if err != nil { |
| 156 | s.Log.Error("authentication failed", err, "username", username, "src_ip", remoteAddr) |
| 157 | if s.ErrorMap != nil { |
| 158 | return s.ErrorMap(ErrInvalidAuthCred) |
| 159 | } |
| 160 | return ErrInvalidAuthCred |
| 161 | } |
| 162 | |
| 163 | return successCb(identity, ContextData{ |
| 164 | Username: username, |
| 165 | Password: password, |
| 166 | }) |
| 167 | }) |
| 168 | case sasl.Login: |
| 169 | if !s.EnableLogin { |
| 170 | return FailingSASLServ{Err: ErrUnsupportedMech} |
| 171 | } |
| 172 | |
| 173 | return sasllogin.NewLoginServer(func(username, password string) error { |
| 174 | username, err := s.usernameForAuth(context.Background(), username) |
| 175 | if err != nil { |
| 176 | if s.ErrorMap != nil { |
| 177 | return s.ErrorMap(ErrInvalidAuthCred) |
| 178 | } |
| 179 | return err |
| 180 | } |
| 181 | |
| 182 | err = s.AuthPlain(username, password) |
| 183 | if err != nil { |
| 184 | s.Log.Error("authentication failed", err, "username", username, "src_ip", remoteAddr) |
| 185 | if s.ErrorMap != nil { |
| 186 | return s.ErrorMap(ErrInvalidAuthCred) |
| 187 | } |
| 188 | return ErrInvalidAuthCred |
| 189 | } |
| 190 | |
| 191 | return successCb(username, ContextData{ |
| 192 | Username: username, |
| 193 | Password: password, |
| 194 | }) |