示例#1
0
文件: box.go 项目: cubeee/go-sig
// 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
}
示例#2
0
文件: multiple.go 项目: cubeee/go-sig
// 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
}
示例#3
0
文件: multiple.go 项目: cubeee/go-sig
func (m MultiGoalGenerator) HandleForm(c web.C, writer http.ResponseWriter, request *http.Request) {
	request.ParseForm()
	form := request.Form
	username := form.Get("username")

	// Workaround to preserve order
	var buf bytes.Buffer
	for id := 0; id < len(util.Skills); id++ {
		skill := form.Get("skill_" + strconv.Itoa(id))
		goal := form.Get("goal_" + strconv.Itoa(id))

		if skill == "" || goal == "" {
			continue
		}

		if _, err := util.GetSkillByName(skill); err != nil {
			continue
		}

		queryPrefix := url.QueryEscape(skill) + "="
		if buf.Len() > 0 {
			buf.WriteByte('&')
		}
		buf.WriteString(queryPrefix)
		buf.WriteString(url.QueryEscape(goal))
	}

	hide_username := form.Get("hide")
	if hide_username == "on" && util.AES_KEY != nil && len(util.AES_KEY) > 0 {
		name, err := util.Encrypt(username)
		if err == nil {
			name = "_" + name
		}
		username = name
	}

	hash := buf.String()
	url := fmt.Sprintf("/multi/%s?%s", username, hash)
	util.ServeResultPage(c, writer, request, url)
}