github.com/polarismesh/polaris@v1.17.8/auth/defaultauth/config.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package defaultauth
    19  
    20  import "errors"
    21  
    22  // AuthOption 鉴权的配置信息
    23  var AuthOption = DefaultAuthConfig()
    24  
    25  // AuthConfig 鉴权配置
    26  type AuthConfig struct {
    27  	// ConsoleOpen 控制台是否开启鉴权
    28  	ConsoleOpen bool `json:"consoleOpen" xml:"consoleOpen"`
    29  	// ClientOpen 是否开启客户端接口鉴权
    30  	ClientOpen bool `json:"clientOpen" xml:"clientOpen"`
    31  	// Salt 相关密码、token加密的salt
    32  	Salt string `json:"salt" xml:"salt"`
    33  	// Strict 是否启用鉴权的严格模式,即对于没有任何鉴权策略的资源,也必须带上正确的token才能操作, 默认关闭
    34  	// Deprecated
    35  	Strict bool `json:"strict"`
    36  	// ConsoleStrict 是否启用鉴权的严格模式,即对于没有任何鉴权策略的资源,也必须带上正确的token才能操作, 默认关闭
    37  	ConsoleStrict bool `json:"consoleStrict"`
    38  	// ClientStrict 是否启用鉴权的严格模式,即对于没有任何鉴权策略的资源,也必须带上正确的token才能操作, 默认关闭
    39  	ClientStrict bool `json:"clientStrict"`
    40  }
    41  
    42  // Verify 检查配置是否合法
    43  func (cfg *AuthConfig) Verify() error {
    44  	k := len(cfg.Salt)
    45  	switch k {
    46  	case 16, 24, 32:
    47  		break
    48  	default:
    49  		return errors.New("[Auth][Config] salt len must 16 | 24 | 32")
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  // DefaultAuthConfig 返回一个默认的鉴权配置
    56  func DefaultAuthConfig() *AuthConfig {
    57  	return &AuthConfig{
    58  		// 针对控制台接口,默认开启鉴权操作
    59  		ConsoleOpen: true,
    60  		// 针对客户端接口,默认不开启鉴权操作
    61  		ClientOpen: false,
    62  		// Salt token 加密 key
    63  		Salt: "polarismesh@2021",
    64  		// 这里默认开启 OpenAPI 的强 Token 检查模式
    65  		ConsoleStrict: true,
    66  		// 客户端接口默认不开启 token 强检查模式
    67  		ClientStrict: false,
    68  	}
    69  }