Example #1
0
func (p *AuthComponent) CheckAuth(payload spirit.Payload) (result interface{}, err error) {

	if result, err = payload.GetData(); err != nil {
		return
	}

	var contexts interface{}
	var headerExist bool
	if contexts, headerExist = payload.GetContext(CtxHttpHeaders); !headerExist {
		err = ErrAuthorizationHeaderNotExist.New()
		return
	}

	if headers, ok := contexts.(map[string]interface{}); !ok {
		err = ErrAuthorizationHeaderNotExist.New()
		return
	} else if v, exist := headers["Authorization"]; !exist {
		err = ErrAuthorizationHeaderNotExist.New()
		return
	} else if authVal, ok := v.(string); !ok {
		err = ErrAuthorizationHeaderNotExist.New()
		return
	} else {
		authVal = strings.TrimLeft(authVal, "Basic ")

		var val []byte
		if val, err = base64.StdEncoding.DecodeString(authVal); err != nil {
			return
		}

		account := strings.Split(string(val), ":")

		if len(account) != 2 {
			err = ErrAuthorizationHeaderFmtError.New()
			return
		}

		if err = p.Auth.CheckAuth(account[0], account[1]); err != nil {
			return
		}
	}

	return
}
Example #2
0
func (p *MNSEncodingComponent) Encode(payload spirit.Payload) (result interface{}, err error) {
	var vData interface{}
	if vData, err = payload.GetData(); err != nil {
		return
	}

	dataKind := reflect.TypeOf(vData).Kind()
	if dataKind == reflect.Ptr ||
		dataKind == reflect.Struct {
		var data []byte
		data, result = xml.Marshal(vData)
		result = data
	} else {
		err = ErrDataTypeIsNotStruct
		return
	}

	return
}
Example #3
0
func (p *MNSEncodingComponent) Decode(payload spirit.Payload) (result interface{}, err error) {
	var vData interface{}
	if vData, err = payload.GetData(); err != nil {
		return
	}

	var resp interface{}

	if p.conf.SingleMessage {
		resp = &ali_mns.MessageReceiveResponse{}
	} else {
		resp = &ali_mns.BatchMessageReceiveResponse{}
	}

	switch data := vData.(type) {
	case string:
		{
			err = xml.Unmarshal([]byte(data), &resp)
		}
	case []byte:
		{
			err = xml.Unmarshal(data, &resp)
		}
	case io.Reader:
		{
			reader := xml.NewDecoder(data)
			err = reader.Decode(&resp)
		}
	default:
		err = ErrDataTypeCouldNotBeDecode
		return
	}

	if err == nil {
		result = resp
	}

	return
}
func (p *HTTPAPIClient) Call(apiName string, payload spirit.Payload, v interface{}) (err error) {
	apiName = strings.TrimSpace(apiName)

	if apiName == "" {
		err = ErrAPINameIsEmpty.New()
		return
	}

	var payloadData interface{}

	if payloadData, err = payload.GetData(); err != nil {
		return
	}

	jsonPayload := JsonPayload{
		Id:      payload.Id(),
		Data:    payloadData,
		Errors:  payload.Errors(),
		Context: payload.Context(),
	}

	var data []byte
	if data, err = json.Marshal(jsonPayload); err != nil {
		return
	}

	postBodyReader := bytes.NewReader(data)

	var req *http.Request
	if req, err = http.NewRequest("POST", p.url, postBodyReader); err != nil {
		err = ErrAPIClientCreateNewRequestFailed.New().Append(err)
		return
	}

	req.Header.Add(p.apiHeaderName, apiName)
	req.Header.Add(HeaderForwardedPayload, "on")

	var resp *http.Response
	if resp, err = p.client.Do(req); err != nil {
		err = ErrAPIClientSendFailed.New(errors.Params{"api": apiName, "url": p.url})
		return
	}

	var body []byte

	if resp != nil {
		defer resp.Body.Close()

		if bBody, e := ioutil.ReadAll(resp.Body); e != nil {
			err = ErrAPIClientReadResponseBodyFailed.New(errors.Params{"api": apiName}).Append(e)
			return
		} else if resp.StatusCode != http.StatusOK {
			err = ErrAPIClientBadStatusCode.New(errors.Params{"api": apiName, "code": resp.StatusCode})
			return
		} else {
			body = bBody
		}

		if v == nil {
			return
		}
	}

	if v == nil {
		return
	}

	var tmpResp struct {
		Code           uint64      `json:"code"`
		ErrorId        string      `json:"error_id,omitempty"`
		ErrorNamespace string      `json:"error_namespace,omitempty"`
		Message        string      `json:"message"`
		Result         interface{} `json:"result"`
	}

	tmpResp.Result = v

	if e := json.Unmarshal(body, &tmpResp); e != nil {
		err = ErrAPIClientResponseUnmarshalFailed.New(errors.Params{"api": apiName, "url": p.url}).Append(e)
		return
	}

	if tmpResp.Code == 0 {
		return
	} else {
		err = errors.NewErrorCode(tmpResp.ErrorId, tmpResp.Code, tmpResp.ErrorNamespace, tmpResp.Message, "", nil)
		return
	}

	return
}