示例#1
0
文件: auth.go 项目: gigforks/gogs
func OauthRedirect(ctx *context.Context) {
	//request token using code
	code := ctx.Query("code")

	v := url.Values{}
	v.Add("code", code)
	v.Add("client_id", setting.Cfg.Section("oauth").Key("CLIENTID").String())
	v.Add("client_secret", setting.Cfg.Section("oauth").Key("CLIENTSECRET").String())
	v.Add("redirect_uri", setting.Cfg.Section("oauth").Key("REDIRECTURL").String())
	v.Add("state", ctx.Session.Get("state").(string))
	tokenResponse, err := http.Post(setting.Cfg.Section("oauth").Key("TOKENURL").String(), "application/x-www-form-urlencoded", strings.NewReader(v.Encode()))

	if err != nil {
		ctx.Handle(500, "post to token url", err)
	}

	type responseData struct {
		AccessToken string            `json:"access_token"`
		TokenType   string            `json:"token_type"`
		Scope       string            `json:"scope"`
		Info        map[string]string `json:"info"`
	}
	data := responseData{}
	decoder := json.NewDecoder(tokenResponse.Body)
	decoder.Decode(&data)

	//get info and set proper variables
	if data.AccessToken == "" {
		ctx.HandleText(500, "client error")
	}
	username := data.Info["username"]
	// client := http.Client{}
	// req, err := http.NewRequest("GET", fmt.Sprintf("https://itsyou.online/users/%s/info", username), nil)
	// req.Header.Add("Accept", "application/json")
	// req.Header.Add("Authorize", fmt.Sprintf("token %s", accessToken))
	// infoResponse, err := client.Do(req)
	// infobd, _ := ioutil.ReadAll(infoResponse.Body)
	passwd := base.GetRandomString(20)

	//add to cookies and check user existence
	u := &models.User{
		Name:     username,
		Email:    fmt.Sprintf("*****@*****.**", username),
		Passwd:   passwd,
		IsActive: !setting.Service.RegisterEmailConfirm,
	}

	err = models.CreateUser(u)

	if err != nil && models.IsErrUserAlreadyExist(err) == false {
		ctx.Handle(500, "create_user_oauth", err)
	}

	// Auto-set admin for the only user.
	if models.CountUsers() == 1 {
		u.IsAdmin = true
		u.IsActive = true
		if err := models.UpdateUser(u); err != nil {
			ctx.Handle(500, "UpdateUser", err)
			return
		}
	}

	log.Trace("Account created: %s", u.Name)
	usr, _ := models.GetUserByName(u.Name)
	usr.IsActive = true
	ctx.Session.Set("uid", usr.Id)
	ctx.Session.Set("uname", usr.Name)

	ctx.Redirect(setting.AppUrl)
}
示例#2
0
文件: auth.go 项目: gigforks/gogs
func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
	ctx.Data["Title"] = ctx.Tr("sign_up")

	ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha

	if setting.Service.DisableRegistration {
		ctx.Error(403)
		return
	}

	if ctx.HasError() {
		ctx.HTML(200, SIGNUP)
		return
	}

	if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
		ctx.Data["Err_Captcha"] = true
		ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), SIGNUP, &form)
		return
	}

	if form.Password != form.Retype {
		ctx.Data["Err_Password"] = true
		ctx.RenderWithErr(ctx.Tr("form.password_not_match"), SIGNUP, &form)
		return
	}

	u := &models.User{
		Name:     form.UserName,
		Email:    form.Email,
		Passwd:   form.Password,
		IsActive: !setting.Service.RegisterEmailConfirm,
	}
	if err := models.CreateUser(u); err != nil {
		switch {
		case models.IsErrUserAlreadyExist(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form)
		case models.IsErrEmailAlreadyUsed(err):
			ctx.Data["Err_Email"] = true
			ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form)
		case models.IsErrNameReserved(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), SIGNUP, &form)
		case models.IsErrNamePatternNotAllowed(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SIGNUP, &form)
		default:
			ctx.Handle(500, "CreateUser", err)
		}
		return
	}
	log.Trace("Account created: %s", u.Name)

	// Auto-set admin for the only user.
	if models.CountUsers() == 1 {
		u.IsAdmin = true
		u.IsActive = true
		if err := models.UpdateUser(u); err != nil {
			ctx.Handle(500, "UpdateUser", err)
			return
		}
	}

	// Send confirmation e-mail, no need for social account.
	if setting.Service.RegisterEmailConfirm && u.Id > 1 {
		mailer.SendActivateAccountMail(ctx.Context, u)
		ctx.Data["IsSendRegisterMail"] = true
		ctx.Data["Email"] = u.Email
		ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
		ctx.HTML(200, ACTIVATE)

		if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
			log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
		}
		return
	}

	ctx.Redirect(setting.AppSubUrl + "/user/login")
}