package aaaa import ( "encoding/json" "fmt" "net/http" "strings" "time" "github.com/go-resty/resty/v2" "ems.agt/lib/log" "ems.agt/lib/services" "ems.agt/restagent/config" ) var ( UriAAAASSO = config.UriPrefix + "/aaaa/{apiVersion}/security/sso" // for external ) var client = resty.New() func init() { /* client. SetTimeout(10 * time.Second). SetRetryCount(1). SetRetryWaitTime(1 * time.Second). SetRetryMaxWaitTime(2 * time.Second). SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) { return 0, errors.New("quota exceeded") }) */ client.SetTimeout(3 * time.Second) } type AAAATicket struct { Ticket string `json:"ticket"` } type SSOResult struct { SSO struct { Result string `json:"result"` ResultMsg string `json:"result_msg"` Ticket string `json:"ticket"` ResultMsgcode string `json:"result_msgcode"` Account []struct { Accid string `json:"accid"` } `json:"account"` } `json:"sso"` } // Get system state from NF/NFs func GetSSOFromAAAA(w http.ResponseWriter, r *http.Request) { log.Debug("GetSSOFromAAAA processing... ") vars := r.URL.Query() ticket := vars["ticket"] if len(ticket) == 0 { services.ResponseNotFound404UriNotExist(w, r) return } log.Debug("ticket:", ticket) log.Debugf("r.RemoteAddr:%s r.Host: %s", r.RemoteAddr, r.Host) var aaaaIp, omcIp string addr := strings.Split(r.RemoteAddr, ":") if len(addr) > 0 { //aaaaIp := r.RemoteAddr[:strings.Index(r.Host, ":")] aaaaIp = addr[0] } addr = strings.Split(r.Host, ":") if len(addr) > 0 { //omcIp := r.Host[:strings.Index(r.Host, ":")] omcIp = addr[0] } log.Debugf("aaaaIp=%s omcIp=%s", aaaaIp, omcIp) requestURI2NF := fmt.Sprintf("http://%s:8080/qryUserByTicket", aaaaIp) log.Debug("requestURI2NF:", requestURI2NF) aaaaTicket := &AAAATicket{ Ticket: ticket[0], } body, err := json.Marshal(aaaaTicket) response, err := client.R(). EnableTrace(). SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}). SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}). SetBody(body). Post(requestURI2NF) if err != nil { log.Error("Get system state from NF is failed:", err) services.ResponseInternalServerError500ProcessError(w, err) return } log.Debug("response:", response) switch response.StatusCode() { case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted: ssoResult := new(SSOResult) json.Unmarshal(response.Body(), ssoResult) var accid string if len(ssoResult.SSO.Account) != 0 { accid = ssoResult.SSO.Account[0].Accid } redirectUrl := fmt.Sprintf("http://%s:8888/home.html?user=%s", omcIp, accid) services.ResponseRedirect(w, redirectUrl) return default: services.ResponseNotFound404UriNotExist(w, r) return } }