github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/auth/auth.go (about)

     1  // Copyright 2015 PingCAP, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package auth
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/pingcap/tidb/parser/format"
    20  )
    21  
    22  const (
    23  	// UserNameMaxLength is the max length of username.
    24  	UserNameMaxLength = 32
    25  	// HostNameMaxLength is the max length of host name.
    26  	HostNameMaxLength = 255
    27  )
    28  
    29  // UserIdentity represents username and hostname.
    30  type UserIdentity struct {
    31  	Username     string
    32  	Hostname     string
    33  	CurrentUser  bool
    34  	AuthUsername string // Username matched in privileges system
    35  	AuthHostname string // Match in privs system (i.e. could be a wildcard)
    36  	AuthPlugin   string // The plugin specified in handshake, only used during authentication.
    37  }
    38  
    39  // Restore implements Node interface.
    40  func (user *UserIdentity) Restore(ctx *format.RestoreCtx) error {
    41  	if user.CurrentUser {
    42  		ctx.WriteKeyWord("CURRENT_USER")
    43  	} else {
    44  		ctx.WriteName(user.Username)
    45  		ctx.WritePlain("@")
    46  		ctx.WriteName(user.Hostname)
    47  	}
    48  	return nil
    49  }
    50  
    51  // String converts UserIdentity to the format user@host.
    52  // It defaults to providing the AuthIdentity (the matching entry in priv tables)
    53  // To use the actual identity use LoginString()
    54  func (user *UserIdentity) String() string {
    55  	// TODO: Escape username and hostname.
    56  	if user == nil {
    57  		return ""
    58  	}
    59  	if user.AuthUsername != "" {
    60  		return fmt.Sprintf("%s@%s", user.AuthUsername, user.AuthHostname)
    61  	}
    62  	return fmt.Sprintf("%s@%s", user.Username, user.Hostname)
    63  }
    64  
    65  // LoginString returns matched identity in user@host format
    66  // It matches the login user.
    67  func (user *UserIdentity) LoginString() string {
    68  	// TODO: Escape username and hostname.
    69  	if user == nil {
    70  		return ""
    71  	}
    72  	return fmt.Sprintf("%s@%s", user.Username, user.Hostname)
    73  }
    74  
    75  // RoleIdentity represents a role name.
    76  type RoleIdentity struct {
    77  	Username string
    78  	Hostname string
    79  }
    80  
    81  // Restore implements Node interface.
    82  func (role *RoleIdentity) Restore(ctx *format.RestoreCtx) error {
    83  	ctx.WriteName(role.Username)
    84  	if role.Hostname != "" {
    85  		ctx.WritePlain("@")
    86  		ctx.WriteName(role.Hostname)
    87  	}
    88  	return nil
    89  }
    90  
    91  // String converts UserIdentity to the format user@host.
    92  func (role *RoleIdentity) String() string {
    93  	// TODO: Escape username and hostname.
    94  	return fmt.Sprintf("`%s`@`%s`", role.Username, role.Hostname)
    95  }