Exemplo n.º 1
0
func (p Resource) initServices() {
	services.GetTenantService()
	services.GetTokenService()
	services.GetUserService()
	services.GetRoleService()
	services.GetAuthService()
}
Exemplo n.º 2
0
// TokenCreateHandler parses the http request and create a new user token.
// Usage :
//		POST /v1/token
// If successful,response code will be set to 201.
func (p *Resource) TokenCreateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("TokenCreateHandler is called!")

	doc := bson.M{}
	decoder := json.NewDecoder(req.Request.Body)
	err := decoder.Decode(&doc)
	if err != nil {
		logrus.Errorf("decode credential err is %v", err)
		response.WriteStatusError(services.TOKEN_ERROR_CREATE, err, resp)
		return
	}

	username, passwd, tenant, paraErr := tokenCreateParamCheck(doc)
	if paraErr != nil {
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, paraErr, resp)
		return
	}

	ret, errorCode, err := services.GetTokenService().TokenCreate(username, passwd, tenant)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(ret, resp)

	return
}
Exemplo n.º 3
0
// TokenValidateHandler parses the http request and return valid user token.
// Usage :
//		GET /v1/token
// If successful,response code will be set to 201.
func (p *Resource) TokenValidateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("TokenValidateHandler is called!")

	var tokenId = req.QueryParameter("token")
	if len(tokenId) <= 0 {
		logrus.Errorln("invalie parameter! tokenId can not be null for token validation")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("invalie parameter! tokenId can not be null for token validation"), resp)
		return
	}

	errorCode, err := services.GetTokenService().TokenValidate(tokenId)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}
	response.WriteSuccess(resp)

}
Exemplo n.º 4
0
func (p *Resource) TokenDetailHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("TokenDetailHandler is called!")

	token := req.HeaderParameter("X-Auth-Token")
	id := req.PathParameter(ParamID)
	if len(id) <= 0 {
		logrus.Warnln("token id should not be null for token detail operation")
		response.WriteStatusError(services.TOKEN_ERROR_GET, errors.New("token id should not be null for get user operation"), resp)
		return
	}

	ret, errorCode, err := services.GetTokenService().TokenDetail(token, id)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(ret, resp)
}
Exemplo n.º 5
0
// TokenReGenerateHandler parses the http request and generate
// another user's token from current token.
// Usage :
//		POST /v1/token/regenerate
// If successful,response code will be set to 201.
func (p *Resource) TokenReGenerateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infoln("TokenReGenerateHandler is called!")
	token := req.HeaderParameter("X-Auth-Token")

	document := bson.M{}
	decoder := json.NewDecoder(req.Request.Body)
	err := decoder.Decode(&document)
	if err != nil {
		logrus.Errorf("decode token generate err is %v", err)
		response.WriteStatusError(services.TOKEN_ERROR_CREATE, err, resp)
		return
	}

	document, err = mejson.Unmarshal(document)
	if err != nil {
		logrus.Errorf("unmarshal token generate err is %v", err)
		response.WriteStatusError(services.TOKEN_ERROR_CREATE, err, resp)
		return
	}

	userid := document["user_id"]
	tenantid := document["tenant_id"]
	if userid == nil || tenantid == nil {
		logrus.Errorln("invalid parameter! user and tenant should not be null!")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("invalid parameter! user and tenant should not be null!"), resp)
		return
	}

	tokenId, errorCode, err := services.GetTokenService().TokenReGenerate(token, userid.(string), tenantid.(string))
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(tokenId, resp)

	return
}