// Parse the request into a signature request func (b BoxGoalGenerator) ParseSignatureRequest(c web.C, r *http.Request) (util.ParsedSignatureRequest, error) { req := util.NewSignatureRequest() username := util.ParseUsername(c.URLParams["username"]) usernameLength := len(username) if !util.UsernameRegex.MatchString(username) { return req, errors.New("Invalid username entered, allowed characters: alphabets, numbers, _ and +") } if usernameLength < 1 || usernameLength > 12 { return req, errors.New("Username has to be between 1 and 12 characters long") } // Read the skill id and make sure it is numeric id, err := strconv.Atoi(c.URLParams["skill"]) var skill util.Skill if err == nil { // Get the skill by id skill, err = util.GetSkillById(id) if err != nil { return req, errors.New(fmt.Sprintf("No skill found for the given id, make sure it is between 0 and %d", len(util.Skills))) } } else { // Get the skill by name skill, err = util.GetSkillByName(c.URLParams["skill"]) if err != nil { return req, errors.New("No skill found for the given skill name") } } // Read the level and make sure it is numeric goal, err := strconv.Atoi(c.URLParams["goal"]) if err != nil { return req, errors.New("Invalid goal entered, make sure it is numeric") } // Make sure the level is within valid bounds if goal < 0 || goal > 200000000 { return req, errors.New("Invalid level/xp goal entered, make sure it 0-200,000,000") } // Switch the goal type if the goal exceeds the maximum skill level goaltype := util.GoalLevel if (skill.Id == util.INVENTION_ID && goal > util.INVENTION_MAX_LEVEL) || (skill.Id != util.INVENTION_ID && goal > util.MAX_LEVEL) { goaltype = util.GoalXP } req.AddProperty("username", username) req.AddProperty("id", id) req.AddProperty("goal", goal) req.AddProperty("skill", skill) req.AddProperty("goaltype", goaltype) return req, nil }
func (g ExampleGenerator) ParseSignatureRequest(c web.C) (util.ParsedSignatureRequest, error) { req := util.NewSignatureRequest() username := c.URLParams["username"] // todo: clean username usernameLength := len(username) if !util.UsernameRegex.MatchString(username) { return req, errors.New("Invalid username entered, allowed characters: alphabets, numbers, _ and +") } if usernameLength < 1 || usernameLength > 12 { return req, errors.New("Username has to be between 1 and 12 characters long") } req.AddProperty("username", username) return req, nil }
// Parse the request into a signature request func (m MultiGoalGenerator) ParseSignatureRequest(c web.C, r *http.Request) (util.ParsedSignatureRequest, error) { req := util.NewSignatureRequest() username := util.ParseUsername(c.URLParams["username"]) usernameLength := len(username) if !util.UsernameRegex.MatchString(username) { return req, errors.New("Invalid username entered, allowed characters: alphabets, numbers, _ and +") } if usernameLength < 1 || usernameLength > 12 { return req, errors.New("Username has to be between 1 and 12 characters long") } goals := []MultiGoal{} params, _ := util.ParseQueryParameters(r.URL.RawQuery) for _, param := range params { skillName, skillGoal := param.Key, param.Value // Make sure the skill is valid skill, err := util.GetSkillByName(skillName) if err != nil { return req, errors.New("No skill found for the given skill name '" + skillName + "'") } // Check if goal has 'k' or 'm' suffix goal, err := util.FromSuffixed(skillGoal) if err != nil { // Make sure the goal is numeric goal, err = strconv.Atoi(skillGoal) } if err != nil { return req, errors.New("Invalid goal entered for " + skillName + ", make sure it is numeric or has 'k'/'m' suffix") } // Make sure the goal is within valid bounds if goal < 0 || goal > 200000000 { return req, errors.New("Invalid level/xp goal entered, make sure it 0-200,000,000") } // Switch the goal type if the goal exceeds the maximum skill level goaltype := util.GetGoalType(skill, goal) goals = append(goals, MultiGoal{skill, goal, goaltype}) } req.AddProperty("username", username) req.AddProperty("goals", goals) return req, nil }