| 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 |
//go:build gssapi && (windows || linux || darwin) |
| 8 |
// +build gssapi |
| 9 |
// +build windows linux darwin |
| 10 |
|
| 11 |
package auth |
| 12 |
|
| 13 |
import ( |
| 14 |
"context" |
| 15 |
"fmt" |
| 16 |
"net" |
| 17 |
"net/http" |
| 18 |
|
| 19 |
"go.mongodb.org/mongo-driver/x/mongo/driver" |
| 20 |
"go.mongodb.org/mongo-driver/x/mongo/driver/auth/internal/gssapi" |
| 21 |
) |
| 22 |
|
| 23 |
// GSSAPI is the mechanism name for GSSAPI. |
| 24 |
const GSSAPI = "GSSAPI" |
| 25 |
|
| 26 |
func newGSSAPIAuthenticator(cred *Cred, _ *http.Client) (Authenticator, error) { |
| 27 |
if cred.Source != "" && cred.Source != sourceExternal { |
| 28 |
return nil, newAuthError("GSSAPI source must be empty or $external", nil) |
| 29 |
} |
| 30 |
|
| 31 |
return &GSSAPIAuthenticator{ |
| 32 |
Username: cred.Username, |
| 33 |
Password: cred.Password, |
| 34 |
PasswordSet: cred.PasswordSet, |
| 35 |
Props: cred.Props, |
| 36 |
}, nil |
| 37 |
} |
| 38 |
|
| 39 |
// GSSAPIAuthenticator uses the GSSAPI algorithm over SASL to authenticate a connection. |
| 40 |
type GSSAPIAuthenticator struct { |
| 41 |
Username string |
| 42 |
Password string |
| 43 |
PasswordSet bool |
| 44 |
Props map[string]string |
| 45 |
} |
| 46 |
|
| 47 |
// Auth authenticates the connection. |
| 48 |
func (a *GSSAPIAuthenticator) Auth(ctx context.Context, cfg *Config) error { |
| 49 |
target := cfg.Description.Addr.String() |
| 50 |
hostname, _, err := net.SplitHostPort(target) |
| 51 |
if err != nil { |
| 52 |
return newAuthError(fmt.Sprintf("invalid endpoint (%s) specified: %s", target, err), nil) |
| 53 |
} |
| 54 |
|
| 55 |
client, err := gssapi.New(hostname, a.Username, a.Password, a.PasswordSet, a.Props) |
| 56 |
|
| 57 |
if err != nil { |
| 58 |
return newAuthError("error creating gssapi", err) |
| 59 |
} |
| 60 |
return ConductSaslConversation(ctx, cfg, sourceExternal, client) |
| 61 |
} |
| 62 |
|
| 63 |
// Reauth reauthenticates the connection. |
| 64 |
func (a *GSSAPIAuthenticator) Reauth(_ context.Context, _ *driver.AuthConfig) error { |
| 65 |
return newAuthError("GSSAPI does not support reauthentication", nil) |
| 66 |
} |