func (p *GeneratePrivateKeyRequestHandler) ContentTypesProvided(req wm.Request, cxt wm.Context) ([]wm.MediaTypeHandler, wm.Request, wm.Context, int, error) { gpkc := cxt.(GeneratePrivateKeyContext) user := gpkc.User() consumer := gpkc.Consumer() var userId, consumerId string if user != nil { userId = user.Id } if consumer != nil { consumerId = consumer.Id } accessKey, err := p.authDS.StoreAccessKey(dm.NewAccessKey(userId, consumerId)) gpkc.SetAccessKey(accessKey) obj := make(map[string]interface{}) if user != nil { obj["user_id"] = user.Id obj["username"] = user.Username obj["name"] = user.Name } if consumer != nil { obj["consumer_id"] = consumer.Id obj["consumer_short_name"] = consumer.ShortName } if accessKey != nil { obj["access_key_id"] = accessKey.Id obj["private_key"] = accessKey.PrivateKey } theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT) jsonObj, _ := theobj.(jsonhelper.JSONObject) if err != nil { return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, time.Time{}, "")}, req, gpkc, http.StatusInternalServerError, err } return []wm.MediaTypeHandler{apiutil.NewJSONMediaTypeHandler(jsonObj, time.Time{}, "")}, req, gpkc, 0, nil }
func (p *LoginAccountRequestHandler) HandleInputHandlerAfterSetup(lac LoginAccountContext) (int, http.Header, io.WriterTo) { errors := make(map[string][]error) user, err := lac.ValidateLogin(p.ds, p.authDS, errors) if len(errors) > 0 { if err != nil { return apiutil.OutputErrorMessage(err.Error(), errors, http.StatusBadRequest, nil) } return apiutil.OutputErrorMessage(ERR_VALUE_ERRORS.Error(), errors, http.StatusUnauthorized, nil) } if err == ERR_INVALID_USERNAME_PASSWORD_COMBO { return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusUnauthorized, nil) } if err != nil { return apiutil.OutputErrorMessage("Unable to process login request: ", nil, http.StatusInternalServerError, nil) } if user == nil { return apiutil.OutputErrorMessage("Unable to process login request: no such username", nil, http.StatusUnauthorized, nil) } accessKey, err := p.authDS.StoreAccessKey(dm.NewAccessKey(user.Id, "")) if err != nil { return apiutil.OutputErrorMessage("Unable to process login request: "+err.Error(), nil, http.StatusInternalServerError, nil) } obj := jsonhelper.NewJSONObject() obj.Set("user_id", user.Id) obj.Set("username", user.Username) obj.Set("name", user.Name) obj.Set("access_key_id", accessKey.Id) obj.Set("private_key", accessKey.PrivateKey) lac.SetResult(obj) return 0, nil, nil }
func (p *CreateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) { cac := cxt.(CreateAccountContext) cac.SetFromJSON(inputObj) cac.CleanInput(cac.RequestingUser()) errors := make(map[string][]error) var obj map[string]interface{} var accessKey *dm.AccessKey var err error ds := p.ds authDS := p.authDS if user := cac.User(); user != nil { var userPassword *dm.UserPassword user.Validate(true, errors) if len(errors) == 0 { user, err = ds.CreateUserAccount(user) if err == nil && user != nil { accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, "")) } } if cac.Password() != "" && user != nil && user.Id != "" { userPassword = dm.NewUserPassword(user.Id, cac.Password()) userPassword.Validate(true, errors) if len(errors) == 0 && err == nil { userPassword, err = authDS.StoreUserPassword(userPassword) } } obj = make(map[string]interface{}) obj["user"] = user obj["type"] = "user" obj["key"] = accessKey } else if user := cac.Consumer(); user != nil { user.Validate(true, errors) if len(errors) == 0 { user, err = ds.CreateConsumerAccount(user) if err == nil && user != nil { accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey("", user.Id)) } } obj = make(map[string]interface{}) obj["consumer"] = user obj["type"] = "consumer" obj["key"] = accessKey } else if user := cac.ExternalUser(); user != nil { user.Validate(true, errors) if len(errors) == 0 { user, err = ds.CreateExternalUserAccount(user) if err == nil && user != nil { accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, user.ConsumerId)) } } obj = make(map[string]interface{}) obj["external_user"] = user obj["type"] = "external_user" obj["key"] = accessKey } else { return apiutil.OutputErrorMessage("\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil) } if len(errors) > 0 { return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil) } if err != nil { return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil) } theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT) jsonObj, _ := theobj.(jsonhelper.JSONObject) return apiutil.OutputJSONObject(jsonObj, cac.LastModified(), cac.ETag(), 0, nil) }