| 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package authscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/ |
| 8 | |
| 9 | import ( |
| 10 | "contextscip-go gomod github.com/golang/go/src go1.18 context/" |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 12 | "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/" |
| 13 | |
| 14 | "go.mongodb.org/mongo-driver/x/mongo/driverscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver`/" |
| 15 | ) |
| 16 | |
| 17 | func newDefaultAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newDefaultAuthenticator().(cred *Credscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/Cred#, httpClient *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Clientscip-go gomod github.com/golang/go/src go1.18 `net/http`/Client#) (Authenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/Authenticator#, error) { |
| 18 | scram, err := newScramSHA256Authenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newScramSHA256Authenticator().(cred, httpClient) |
| 19 | if err != nil { |
| 20 | return nil, newAuthErrorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newAuthError().("failed to create internal authenticator", err) |
| 21 | } |
| 22 | speculative, ok := scram.(SpeculativeAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SpeculativeAuthenticator#) |
| 23 | if !ok { |
| 24 | typeErr := fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("expected SCRAM authenticator to be SpeculativeAuthenticator but got %T", scram) |
| 25 | return nil, newAuthErrorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newAuthError().("failed to create internal authenticator", typeErr) |
| 26 | } |
| 27 | |
| 28 | return &DefaultAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#{ |
| 29 | Credscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#Cred.: cred, |
| 30 | speculativeAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#speculativeAuthenticator.: speculative, |
| 31 | httpClientscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#httpClient.: httpClient, |
| 32 | }, nil |
| 33 | } |
| 34 | |
| 35 | // DefaultAuthenticator uses SCRAM-SHA-1 or MONGODB-CR depending |
| 36 | // on the server version. |
| 37 | type DefaultAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator# struct { |
| 38 | Credscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#Cred. *Credscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/Cred# |
| 39 | |
| 40 | // The authenticator to use for speculative authentication. Because the correct auth mechanism is unknown when doing |
| 41 | // the initial hello, SCRAM-SHA-256 is used for the speculative attempt. |
| 42 | speculativeAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#speculativeAuthenticator. SpeculativeAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SpeculativeAuthenticator# |
| 43 | |
| 44 | httpClientscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#httpClient. *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Clientscip-go gomod github.com/golang/go/src go1.18 `net/http`/Client# |
| 45 | } |
| 46 | |
| 47 | var _ SpeculativeAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SpeculativeAuthenticator# = (*DefaultAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#)(nil) |
| 48 | |
| 49 | // CreateSpeculativeConversation creates a speculative conversation for SCRAM authentication. |
| 50 | func (a *DefaultAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#) CreateSpeculativeConversationscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#CreateSpeculativeConversation().() (SpeculativeConversationscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SpeculativeConversation#, error) { |
| 51 | return a.speculativeAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#speculativeAuthenticator..CreateSpeculativeConversationscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SpeculativeAuthenticator#CreateSpeculativeConversation.() |
| 52 | } |
| 53 | |
| 54 | // Auth authenticates the connection. |
| 55 | func (a *DefaultAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#) Authscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#Auth().(ctx contextscip-go gomod github.com/golang/go/src go1.18 context/.Contextscip-go gomod github.com/golang/go/src go1.18 context/Context#, cfg *Configscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/Config#) error { |
| 56 | var actual Authenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/Authenticator# |
| 57 | var err error |
| 58 | |
| 59 | switch chooseAuthMechanismscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/chooseAuthMechanism().(cfg) { |
| 60 | case SCRAMSHA256scip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SCRAMSHA256.: |
| 61 | actual, err = newScramSHA256Authenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newScramSHA256Authenticator().(a.Credscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#Cred., a.httpClientscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#httpClient.) |
| 62 | case SCRAMSHA1scip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SCRAMSHA1.: |
| 63 | actual, err = newScramSHA1Authenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newScramSHA1Authenticator().(a.Credscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#Cred., a.httpClientscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#httpClient.) |
| 64 | default: |
| 65 | actual, err = newMongoDBCRAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newMongoDBCRAuthenticator().(a.Credscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#Cred., a.httpClientscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#httpClient.) |
| 66 | } |
| 67 | |
| 68 | if err != nil { |
| 69 | return newAuthErrorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newAuthError().("error creating authenticator", err) |
| 70 | } |
| 71 | |
| 72 | return actual.Authscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver`/Authenticator#Auth.(ctx, cfg) |
| 73 | } |
| 74 | |
| 75 | // Reauth reauthenticates the connection. |
| 76 | func (a *DefaultAuthenticatorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#) Reauthscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/DefaultAuthenticator#Reauth().(_ contextscip-go gomod github.com/golang/go/src go1.18 context/.Contextscip-go gomod github.com/golang/go/src go1.18 context/Context#, _ *driverscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver`/.AuthConfigscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver`/AuthConfig#) error { |
| 77 | return newAuthErrorscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/newAuthError().("DefaultAuthenticator does not support reauthentication", nil) |
| 78 | } |
| 79 | |
| 80 | // If a server provides a list of supported mechanisms, we choose |
| 81 | // SCRAM-SHA-256 if it exists or else MUST use SCRAM-SHA-1. |
| 82 | // Otherwise, we decide based on what is supported. |
| 83 | func chooseAuthMechanismscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/chooseAuthMechanism().(cfg *Configscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/Config#) string { |
| 84 | if saslSupportedMechs := cfg.HandshakeInfoscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver`/AuthConfig#HandshakeInfo..SaslSupportedMechsscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver`/HandshakeInformation#SaslSupportedMechs.; saslSupportedMechs != nil { |
| 85 | for _, v := range saslSupportedMechs { |
| 86 | if v == SCRAMSHA256scip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SCRAMSHA256. { |
| 87 | return v |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return SCRAMSHA1scip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/x/mongo/driver/auth`/SCRAMSHA1. |
| 93 | } |