|
@@ -0,0 +1,166 @@
|
|
|
|
+package baosteel
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "eta/eta_bridge/global"
|
|
|
|
+ "fmt"
|
|
|
|
+ "io"
|
|
|
|
+ "net/http"
|
|
|
|
+ "strings"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+func HttpGetAuth(urlPath string) ([]byte, error) {
|
|
|
|
+ if global.CONFIG.Baosteel.UserAuthHost == `` {
|
|
|
|
+ return nil, fmt.Errorf("统一认证地址为空")
|
|
|
|
+ }
|
|
|
|
+ getUrl := global.CONFIG.Baosteel.UserAuthHost + urlPath
|
|
|
|
+
|
|
|
|
+ client := &http.Client{}
|
|
|
|
+ req, err := http.NewRequest("GET", getUrl, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ resp, err := client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ result, err := io.ReadAll(resp.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ tips := fmt.Sprintf("Baosteel统一认证: %s;\n返回参数:%s\n", getUrl, string(result))
|
|
|
|
+ global.FILE_LOG.Debug(tips)
|
|
|
|
+
|
|
|
|
+ // 解析返回参数,判断是否是json
|
|
|
|
+ if !json.Valid(result) {
|
|
|
|
+ err = fmt.Errorf("返回参数不是json格式")
|
|
|
|
+ }
|
|
|
|
+ return result, err
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func HttpPostAuth(urlPath, postData, accessToken string, params ...string) ([]byte, error) {
|
|
|
|
+ if global.CONFIG.Baosteel.UserAuthHost == `` {
|
|
|
|
+ return nil, fmt.Errorf("统一认证地址为空")
|
|
|
|
+ }
|
|
|
|
+ postUrl := global.CONFIG.Baosteel.UserAuthHost + urlPath
|
|
|
|
+
|
|
|
|
+ body := io.NopCloser(strings.NewReader(postData))
|
|
|
|
+ client := &http.Client{}
|
|
|
|
+ req, err := http.NewRequest("POST", postUrl, body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ contentType := "application/x-www-form-urlencoded;charset=utf-8"
|
|
|
|
+ if len(params) > 0 && params[0] != "" {
|
|
|
|
+ contentType = params[0]
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("content-Type", contentType)
|
|
|
|
+ if accessToken != "" {
|
|
|
|
+ token := fmt.Sprintf(`Bearer %s`, accessToken)
|
|
|
|
+ req.Header.Set("Authorization", token)
|
|
|
|
+ }
|
|
|
|
+ resp, err := client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ result, err := io.ReadAll(resp.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ tips := fmt.Sprintf("Baosteel统一认证: %s;\n请求参数:%s;\n返回参数:%s\n", postUrl, postData, string(result))
|
|
|
|
+ global.FILE_LOG.Debug(tips)
|
|
|
|
+
|
|
|
|
+ // 解析返回参数,判断是否是json
|
|
|
|
+ if !json.Valid(result) {
|
|
|
|
+ err = fmt.Errorf("返回参数不是json格式")
|
|
|
|
+ }
|
|
|
|
+ return result, err
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type GetTokenResp struct {
|
|
|
|
+ AccessToken string `json:"access_token"`
|
|
|
|
+ RefreshToken string `json:"refresh_token"`
|
|
|
|
+ ExpiresIn int `json:"expires_in"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetToken(code string) (resp *GetTokenResp, err error) {
|
|
|
|
+ // http://eplat.baogang.info/base-security-service/oauth/token?client_id=piosbguy844Y83wv2Ma26Y &client_secret=3AE9D870EEBF59F34F697E6DE531933D&code=dy9KF1&grant_type=authorization _code&redirect_uri=http://10.70.16.180:8080/apps/pios/index
|
|
|
|
+ urlPath := fmt.Sprintf(`/oauth/token?client_id=%s&client_secret=%s&code=%s&grant_type=authorization_code&redirect_uri=%s`, global.CONFIG.Baosteel.UserAuthClientId, global.CONFIG.Baosteel.UserAuthClientSecret, code, global.CONFIG.Baosteel.UserAuthRedirectUrl)
|
|
|
|
+
|
|
|
|
+ result, e := HttpPostAuth(urlPath, ``, "", "application/json")
|
|
|
|
+ if e != nil {
|
|
|
|
+ err = fmt.Errorf("http post err: %v", e)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if e = json.Unmarshal(result, &resp); e != nil {
|
|
|
|
+ err = fmt.Errorf("result unmarshal err: %v", e)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ //global.FILE_LOG.Debug(fmt.Sprintf("Baosteel GetToken Resp: %s", string(result)))
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type UserInfoResp struct {
|
|
|
|
+ Birthday string `json:"birthday"`
|
|
|
|
+ Gender int `json:"gender"`
|
|
|
|
+ IdCardNo string `json:"idCardNo"`
|
|
|
|
+ Mobile string `json:"mobile"`
|
|
|
|
+ HeadUrl string `json:"headUrl"`
|
|
|
|
+ UserName string `json:"userName"`
|
|
|
|
+ UserId string `json:"userId"`
|
|
|
|
+ RealName string `json:"realName"`
|
|
|
|
+ LoginName string `json:"loginName"`
|
|
|
|
+ IsLocked string `json:"isLocked"`
|
|
|
|
+ ContactAddress string `json:"contactAddress"`
|
|
|
|
+ Email string `json:"email"`
|
|
|
|
+ Age int `json:"age"`
|
|
|
|
+ Status string `json:"status"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetUserInfo(token string) (resp *UserInfoResp, err error) {
|
|
|
|
+ // http://eplat.baogang.info/base-security-service/user
|
|
|
|
+ urlPath := `/user`
|
|
|
|
+
|
|
|
|
+ // access_token在header中
|
|
|
|
+ result, e := HttpPostAuth(urlPath, "", token, "application/json")
|
|
|
|
+ if e != nil {
|
|
|
|
+ err = fmt.Errorf("http post err: %v", e)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if e = json.Unmarshal(result, &resp); e != nil {
|
|
|
|
+ err = fmt.Errorf("result unmarshal err: %v", e)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ //global.FILE_LOG.Debug(fmt.Sprintf("Baosteel GetUserInfo Resp: %s", string(result)))
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func RefreshToken(refreshToken string) (err error) {
|
|
|
|
+ // http://eplat.baogang.info/base-security-service/oauth/token?client_id=piosbguy844Y83wv2Ma26Y&client_secret=3AE9D870EEBF59F34F697E6DE531933D&grant_type=refresh_token&refresh_toke n=eyJhbGciOiJSUzI1NiIsInR5cCI6Ikp
|
|
|
|
+ urlPath := fmt.Sprintf(`/oauth/token?client_id=%s&client_secret=%s&code=%s&grant_type=refresh_token&refresh_toke=%s`, global.CONFIG.Baosteel.UserAuthClientId, global.CONFIG.Baosteel.UserAuthClientSecret, refreshToken, global.CONFIG.Baosteel.UserAuthRedirectUrl)
|
|
|
|
+
|
|
|
|
+ _, e := HttpPostAuth(urlPath, ``, "", "application/json")
|
|
|
|
+ if e != nil {
|
|
|
|
+ err = fmt.Errorf("http post err: %v", e)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ //global.FILE_LOG.Debug(fmt.Sprintf("Baosteel RefreshToken Resp: %s", string(result)))
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func RevokeToken(token string) (err error) {
|
|
|
|
+ // http://eplat.baogang.info/base-security-service/logout?callback=http://10.70.16.180:8080/apps/pios&token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnRJZCI6InBpb3NiZ3V5ODQ0WTgzd3YyT WEyNlkiLCJ1c2VyX25hbWUiOiI3MDI0ODEiLCJsb2dpbk5hbWUiOiI3MDI0ODEiLCJleHAiOjE3NDEzMT U4NzIsInVzZXJOYW1lIjoi5b6Q6ICAIiwidHlwZSI6InVzZXIiLCJ1c2VySWQiOiI3MDI
|
|
|
|
+ urlPath := fmt.Sprintf(`/logout?callback=%s&token=%s`, global.CONFIG.Baosteel.UserAuthCallbackUrl, token)
|
|
|
|
+
|
|
|
|
+ _, e := HttpPostAuth(urlPath, ``, "", "application/json")
|
|
|
|
+ if e != nil {
|
|
|
|
+ err = fmt.Errorf("http post err: %v", e)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ //global.FILE_LOG.Debug(fmt.Sprintf("Baosteel RevokeToken Resp: %s", string(result)))
|
|
|
|
+ return
|
|
|
|
+}
|