...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/auth/gssapi.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/auth

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

View as plain text