Пример #1
0
func GetAccess(token string) (access *osin.AccessData, err error) {
	url := fmt.Sprintf("http://%s/access?token=%s", oauthAddress, token)

	request, err := http.NewRequest("GET", url, nil)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	httpClient := &http.Client{}
	response, err := httpClient.Do(request)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	switch response.StatusCode {
	case 200:
		// Correct, do nothing.
	case 400:
		return nil, global.ErrInvalidToken
	default:
		return nil, errors.New("Invalid status code: " + response.Status)
	}

	access = new(osin.AccessData)

	jdec := json.NewDecoder(response.Body)
	err = jdec.Decode(&access)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	return
}
Пример #2
0
func GrantAccessTokenWithClientCredentials(userId string) (responseData osin.ResponseData, err error) {

	url := fmt.Sprintf("http://%s/token?grant_type=client_credentials&user_id=%s", oauthAddress, userId)

	request, err := http.NewRequest("GET", url, nil)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	request.SetBasicAuth(OfficalClient.GetId(), OfficalClient.GetSecret())

	httpClient := &http.Client{}
	response, err := httpClient.Do(request)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	if response.StatusCode != 200 {
		err = errors.New("Invalid status code " + response.Status)
		return
	}

	responseData = make(osin.ResponseData)

	jdec := json.NewDecoder(response.Body)
	err = jdec.Decode(&responseData)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	return
}
Пример #3
0
func CallRpcService(env Env, name, method string, req interface{}, res rpcResponse) (err error) {
	// Get the service TCP client
	client, err := getEnvTcpClient(name, env)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	err = client.Call(method, req, res)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	return
}
Пример #4
0
func getEnvTcpClient(serviceName string, env Env) (client *rpc.Client, err error) {

	var addr string

	switch {
	case env.IsDev(), env.IsTest():

		addr, _ = global.ServiceMap[serviceName]
		if addr == "" {
			err = global.ErrServiceNotFound
			return
		}

	case env.IsProd():
		addr = serviceName + ":80"
	default:
		err = global.ErrInvalidEnv
		return
	}

	client, err = rpc.Dial("tcp", addr)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}

	return
}
Пример #5
0
func CallRpcServiceWithContext(c *gin.Context, env Env, name, method string, req interface{}, res rpcResponse) bool {

	// Get the service TCP client
	client, err := getEnvTcpClient(name, env)
	if goutils.HasErrorAndPrintStack(err) {
		c.AbortWithError(http.StatusInternalServerError, err)
		return true
	}

	err = client.Call(method, req, res)
	if err != nil {

		// Check if it is not found error
		if err.Error() == "not found" {
			c.AbortWithStatus(http.StatusNotFound)
		} else {
			goutils.PrintStackAndError(err)
			c.AbortWithError(http.StatusInternalServerError, err)
		}
		return true
	}

	if res.HasError() {
		c.JSON(http.StatusBadRequest, res.ReturnErrorMap())
		return true
	}

	return false
}
Пример #6
0
func EncryptPassword(pwd string) (encryptedPwd string, err error) {
	hp, err := bcrypt.GenerateFromPassword([]byte(pwd), 0)
	if goutils.HasErrorAndPrintStack(err) {
		return
	}
	encryptedPwd = string(hp)
	return
}
Пример #7
0
func (this *rpcEngine) Run() {

	// Register the runtime rpc methods for changing env when runing tests
	this.RegisterName("Runtime", this.runtime)

	var port = this.runtime.Config.GetRpcPort()
	log.Printf("Listening and serving TCP on %s\n", port)
	listen, err := net.Listen("tcp", port)
	if err != nil {
		panic(err)
	}

	for {
		conn, err := listen.Accept()
		if goutils.HasErrorAndPrintStack(err) {
			continue
		}

		go this.ServeConn(conn)
	}
}