Skip to main content

co

广东政数局密改模块

package main

import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/tjfoc/gmsm/sm3"
)

// type RequestBody struct {
// GrantType string
// Credentials string
// }

type Credentials struct {
Algorithm string `json:"algorithm"`
Response string `json:"response"`
UserId string `json:"user_id"`
ServerId string `json:"server_id"`
TimeStamp int64 `json:"timestamp"`
ServerRandom string `json:"server_random"`
}

type ResponseBody struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpriresIn int `json:"exprise_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
DigestKey string `json:"digest_key"`
}

func GetAccessToken() (string, error) {

//7BZBrtWNYsBIpNCx
constValue := &Credentials{
Algorithm: "15843-4-5.1.1",
UserId: "67edf763773b6d69d3885fc1",
ServerId: "auth_center",
TimeStamp: time.Now().UnixMilli(),
}

is := strconv.Itoa(int(constValue.TimeStamp))
wb := fmt.Sprintf("%s%s%s", is, "auth_center", "1nDXPhzCbcoW37iICLoV0WanNDRQAFEj")

h := sm3.New()
h.Write([]byte(wb))
digest := h.Sum(nil)

constValue.Response = base64.StdEncoding.EncodeToString(digest)

fmt.Printf("constValue: %v\n", constValue)

bv, err := json.Marshal(constValue)
if err != nil {
return "", err
}

bsv := Base64Encode(string(bv))

data := url.Values{}
data.Add("grant_type", "password")
data.Add("credentials", bsv)
formData := strings.NewReader(data.Encode())

fmt.Printf("formData: %v\n", formData)

client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

AuthString := fmt.Sprintf("Basic %s", Base64Encode("7BZBrtWNYsBIpNCx:QAuEwZWT6NTU4FEJjoXbfXRPPIxYigIr"))
req, err := http.NewRequest("POST", "https://19.15.47.83:443/auth_center/cipher/token", formData)
if err != nil {
return "", err
}

fmt.Println("---------------------------GetToken------------------------------------")
fmt.Println("req: ", req)
fmt.Println("-----------------------------------------------------------------------")

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "MyGoClient/1.0")
req.Header.Set("Authorization", AuthString)

resp, err := client.Do(req)
if err != nil {
return "", err
}
fmt.Println("--------------------------------------------------------------------")
fmt.Println("resp: ", resp)
fmt.Println("--------------------------------------------------------------------")
defer resp.Body.Close()

body, _ := io.ReadAll(resp.Body)

fmt.Println("body:", string(body))

var rsp ResponseBody
if err := json.Unmarshal(body, &rsp); err != nil {
return "", err
}

fmt.Println("--------------------------------------------------------------------")
fmt.Println("rsp: ", rsp)
fmt.Println("rsp.AccessToken: ", rsp.AccessToken)
fmt.Println("--------------------------------------------------------------------")

return rsp.AccessToken, nil
}

func Base64Encode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}

func main() {
//TIP <p>Press <shortcut actionId="ShowIntentionActions"/> when your caret is at the underlined text
// to see how GoLand suggests fixing the warning.</p><p>Alternatively, if available, click the lightbulb to view possible fixes.</p>
response, _ := GetAccessToken()
fmt.Println("Hello and welcome, %s!", response)

}