github.com/polarismesh/polaris@v1.17.8/auth/defaultauth/token_test.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_test 19 20 import ( 21 "fmt" 22 "testing" 23 "time" 24 25 "golang.org/x/crypto/bcrypt" 26 27 "github.com/polarismesh/polaris/auth/defaultauth" 28 ) 29 30 // Test_CustomDesignSalt 主要用于有自定义salt需求的用户 31 func Test_CustomDesignSalt(t *testing.T) { 32 defaultauth.AuthOption = defaultauth.DefaultAuthConfig() 33 // 设置自定义的 salt 值,长度需要满足在 [16, 24, 32] 中的任意一个 34 defaultauth.AuthOption.Salt = "polarismesh@2021" 35 36 // polaris 用户的ID 37 uid := "65e4789a6d5b49669adf1e9e8387549c" 38 fmt.Printf("uid=%s\n", uid) 39 40 token, err := defaultauth.TestCreateToken(uid, "") 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 // 输出最终的 token 信息 46 fmt.Printf("token=%s\n", token) 47 48 // 对 polaris 用户的密码进行加密 49 password, err := bcrypt.GenerateFromPassword([]byte("polaris"), bcrypt.DefaultCost) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 // 输出最终的密码值 55 fmt.Printf("password=%s\n", string(password)) 56 57 time.Sleep(time.Second) 58 } 59 60 func TestCreateToken(t *testing.T) { 61 defaultauth.AuthOption = defaultauth.DefaultAuthConfig() 62 defaultauth.AuthOption.Salt = "polarismesh@2021" 63 64 uid := "65e4789a6d5b49669adf1e9e8387549c" 65 fmt.Printf("uid=%s\n", uid) 66 67 token, err := defaultauth.TestCreateToken(uid, "") 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 fmt.Printf("token=%s\n", token) 73 74 password, err := bcrypt.GenerateFromPassword([]byte("polaris"), bcrypt.DefaultCost) 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 fmt.Printf("password=%s\n", string(password)) 80 81 time.Sleep(time.Second) 82 } 83 84 func TestDecodeToken(t *testing.T) { 85 token := "bRJ76j5/mXQ1RFS0fs1vYIlcmGljGJ2W/CYKBKNVAdLGlW1otecX2qVQF0khlISq0q1r2v4fkI0o8OrhWcE=" 86 v, err := defaultauth.TestDecryptMessage([]byte("polaris@acb04d93c6e14bc1"), token) 87 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 t.Log(v) 93 }