github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/create_user_data.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package plan
    16  
    17  import (
    18  	"crypto/sha1"
    19  	"encoding/hex"
    20  	"fmt"
    21  	"strings"
    22  )
    23  
    24  // UserName represents either a user or role name.
    25  type UserName struct {
    26  	Name    string
    27  	Host    string
    28  	AnyHost bool
    29  }
    30  
    31  // String returns the UserName as a formatted string using the quotes given. Using the default root
    32  // account with the backtick as the quote, root@localhost would become `root`@`localhost`. Different quotes are used
    33  // in different places in MySQL. In addition, if the quote is used in a section as part of the name, it is escaped by
    34  // doubling the quote (which also mimics MySQL behavior).
    35  func (un *UserName) String(quote string) string {
    36  	host := un.Host
    37  	if un.AnyHost {
    38  		host = "%"
    39  	}
    40  	replacement := quote + quote
    41  	name := strings.ReplaceAll(un.Name, quote, replacement)
    42  	host = strings.ReplaceAll(host, quote, replacement)
    43  	return fmt.Sprintf("%s%s%s@%s%s%s", quote, name, quote, quote, host, quote)
    44  }
    45  
    46  // Authentication represents an authentication method for a user.
    47  type Authentication interface {
    48  	// Plugin returns the name of the plugin that this authentication represents.
    49  	Plugin() string
    50  	// Password returns the value to insert into the database as the password.
    51  	Password() string
    52  }
    53  
    54  // AuthenticatedUser represents a user with the relevant methods of authentication.
    55  type AuthenticatedUser struct {
    56  	UserName
    57  	Auth1       Authentication
    58  	Auth2       Authentication
    59  	Auth3       Authentication
    60  	AuthInitial Authentication
    61  	Identity    string
    62  }
    63  
    64  // TLSOptions represents a user's TLS options.
    65  type TLSOptions struct {
    66  	SSL     bool
    67  	X509    bool
    68  	Cipher  string
    69  	Issuer  string
    70  	Subject string
    71  }
    72  
    73  // AccountLimits represents the limits imposed upon an account.
    74  type AccountLimits struct {
    75  	MaxQueriesPerHour     *int64
    76  	MaxUpdatesPerHour     *int64
    77  	MaxConnectionsPerHour *int64
    78  	MaxUserConnections    *int64
    79  }
    80  
    81  // PasswordOptions states how to handle a user's passwords.
    82  type PasswordOptions struct {
    83  	RequireCurrentOptional bool
    84  
    85  	ExpirationTime *int64
    86  	History        *int64
    87  	ReuseInterval  *int64
    88  	FailedAttempts *int64
    89  	LockTime       *int64
    90  }
    91  
    92  // AuthenticationMysqlNativePassword is an authentication type that represents "mysql_native_password".
    93  type AuthenticationMysqlNativePassword string
    94  
    95  var _ Authentication = AuthenticationMysqlNativePassword("")
    96  
    97  // Plugin implements the interface Authentication.
    98  func (a AuthenticationMysqlNativePassword) Plugin() string {
    99  	return "mysql_native_password"
   100  }
   101  
   102  // Password implements the interface Authentication.
   103  func (a AuthenticationMysqlNativePassword) Password() string {
   104  	if len(a) == 0 {
   105  		return ""
   106  	}
   107  	// native = sha1(sha1(password))
   108  	hash := sha1.New()
   109  	hash.Write([]byte(a))
   110  	s1 := hash.Sum(nil)
   111  	hash.Reset()
   112  	hash.Write(s1)
   113  	s2 := hash.Sum(nil)
   114  	return "*" + strings.ToUpper(hex.EncodeToString(s2))
   115  }
   116  
   117  // NewDefaultAuthentication returns the given password with the default
   118  // authentication method.
   119  func NewDefaultAuthentication(password string) Authentication {
   120  	return AuthenticationMysqlNativePassword(password)
   121  }
   122  
   123  // AuthenticationOther is an authentication type that represents plugin types
   124  // other than "mysql_native_password". There must be a mysqldb plugin provided
   125  // to use this plugin.
   126  type AuthenticationOther struct {
   127  	password string
   128  	plugin   string
   129  }
   130  
   131  func NewOtherAuthentication(password, plugin string) Authentication {
   132  	return AuthenticationOther{password, plugin}
   133  }
   134  
   135  func (a AuthenticationOther) Plugin() string {
   136  	return a.plugin
   137  }
   138  
   139  func (a AuthenticationOther) Password() string {
   140  	return string(a.password)
   141  }