package service import ( "encoding/base64" "encoding/json" "fmt" log "github.com/sirupsen/logrus" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" "kpt.xdmy/middleware/jwt" "kpt.xdmy/pkg/logging" "kpt.xdmy/pkg/setting" "kpt.xdmy/pkg/util" "net" "time" ) func (s *Service) OAuth2Token(code, host string) (*http.OAuth2TokenResp, error) { authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(setting.IDaaSSetting.ClientId+":"+setting.IDaaSSetting.ClientSecret))) resp := new(http.OAuth2TokenResp) fmt.Println(code, time.Now()) //host = "http://tmrwatch.cn:8082" if host == "eam" { host = "https://192-168-61-418000-8jyo36i0p8n4to.ztna-dingtalk.com" url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/token?&grant_type=%s&code=%s&redirect_uri=%s", "authorization_code", code, fmt.Sprintf("%s/api/v1/oauth2/token", host)) respByte, err := jwt.PostPush(url, nil, authorization) if err != nil { host = "https://eam.modernfarming.cn:8000" url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/token?&grant_type=%s&code=%s&redirect_uri=%s", "authorization_code", code, fmt.Sprintf("%s/api/v1/oauth2/token", host)) respByte, err := jwt.PostPush(url, nil, authorization) if err != nil { log.Error(err) return resp, err } err = json.Unmarshal(respByte, &resp) if err != nil { log.Error(err) return resp, err } return resp, nil } err = json.Unmarshal(respByte, &resp) if err != nil { log.Error(err) return resp, err } return resp, nil } fmt.Println(fmt.Sprintf("%s/api/v1/oauth2/token", host)) url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/token?&grant_type=%s&code=%s&redirect_uri=%s", "authorization_code", code, fmt.Sprintf("%s/api/v1/oauth2/token", host)) respByte, err := jwt.PostPush(url, nil, authorization) if err != nil { log.Error(err) return resp, err } err = json.Unmarshal(respByte, &resp) if err != nil { log.Error(err) return resp, err } return resp, nil } func (s *Service) OAuth2Logout(token, host string) error { authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(setting.IDaaSSetting.ClientId+":"+setting.IDaaSSetting.ClientSecret))) url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/introspect?token=%s&token_type_hint=%s", token, "access_token") _, err := jwt.PostPush(url, nil, authorization) if err != nil { log.Error(err) return err } //host = "http://tmrwatch.cn:8082" if host == "eam" { host = "https://192-168-61-418000-8jyo36i0p8n4to.ztna-dingtalk.com" _, err = jwt.PostPush(fmt.Sprintf("%s/api/v1/logout?redirect_url=%s&client_id=%s", setting.IDaaSSetting.Url, host, setting.IDaaSSetting.ClientId), nil, "") if err != nil { host = "https://eam.modernfarming.cn:8000" _, err = jwt.PostPush(fmt.Sprintf("%s/api/v1/logout?redirect_url=%s&client_id=%s", setting.IDaaSSetting.Url, host, setting.IDaaSSetting.ClientId), nil, "") if err != nil { log.Error(err) return err } } return nil } _, err = jwt.PostPush(fmt.Sprintf("%s/api/v1/logout?redirect_url=%s&client_id=%s", setting.IDaaSSetting.Url, host, setting.IDaaSSetting.ClientId), nil, "") if err != nil { log.Error(err) return err } return nil } func getHostIp() string { addrList, err := net.InterfaceAddrs() if err != nil { fmt.Println("get current host ip err: ", err) return "" } var ip string for _, address := range addrList { if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { if ipNet.IP.To4() != nil { ip = ipNet.IP.String() break } } } return ip } func (s *Service) OAuth2AddUser(userInfo *http.OAuth2AddUser) (*model.UserInfo, error) { var userCount, empCount int64 err := s.d.DB.Table("emp").Where("empCode = ? ", userInfo.Username).Count(&empCount).Error if err != nil { logging.Error(err) return nil, err } emp1 := new(model.Emp) s.d.DB.Table("emp").Where("empCode = ? ", userInfo.Username).First(&emp1) pasture := new(model.Pasture) if userInfo.Pasture != "" { err = s.d.DB.Where("name = ? ", userInfo.Pasture).First(&pasture).Error if err != nil { logging.Error(err) return nil, err } } emp := new(model.Emp) emp.Empname = userInfo.Name emp.EmpCode = userInfo.Username emp.Empname = userInfo.Name if !userInfo.Disabled { emp.Work = 1 } else { emp.Work = 0 } if empCount == 0 { emp.PastureId = "18" } else { emp.PastureId = emp1.PastureId } if empCount == 0 { err = s.d.DB.Create(&emp).Error if err != nil { logging.Error(err) return nil, err } } else { fmt.Println(emp) err = s.d.DB.Debug().Exec(` update emp set work = ? ,pastureId = ? ,empname = ? where empCode = ? `, emp.Work, emp.PastureId, emp.Empname, userInfo.Username).Error if err != nil { logging.Error(err) return nil, err } err = s.d.DB.Where("empCode = ? ", userInfo.Username).First(&emp).Error if err != nil { logging.Error(err) return nil, err } } err = s.d.DB.Table("user").Where("username = ? ", userInfo.Username).Count(&userCount).Error if err != nil { logging.Error(err) return nil, err } user := new(model.UserInfo) user.Empid = emp.Id user.Username = userInfo.Username user.Enable = emp.Work user.Idaas = userInfo.UserId if userInfo.PassWord != "" { user.PassWord = util.EncodeMD5(userInfo.PassWord) } if userCount == 0 { err = s.d.DB.Create(&user).Error if err != nil { logging.Error(err) return user, err } } else { err = s.d.DB.Debug().Exec(`UPDATE user SET empid =?, password=?,enable = ? WHERE username = ? `, user.Empid, user.PassWord, user.Enable, user.Username).Error if err != nil { logging.Error(err) return user, err } err = s.d.DB.Where(" username = ? ", user.Username).First(&user).Error if err != nil { logging.Error(err) return user, err } } return user, nil }