func Openid(c *common.HTTPContext) (err error) { redirect, email, ok, err := gopenid.VerifyAuth(c.Req()) if err != nil { return } if ok { email = strings.ToLower(email) c.Session().Values[common.SessionEmail] = email u := &User{Id: kol.Id(email)} err = c.DB().Get(u) if err == kol.NotFound { err = nil u.Email = email u.Ranking = 1 } if err == nil { u.Language = common.GetLanguage(c.Req()) u.DiplicityHost = c.Req().Host u.LastLoginAt = time.Now() err = c.DB().Set(u) } } else { delete(c.Session().Values, common.SessionEmail) } c.Close() c.Resp().Header().Set("Location", redirect.String()) c.Resp().WriteHeader(302) fmt.Fprintln(c.Resp(), redirect.String()) return }
func OAuth2Callback(clientId, clientSecret string) func(c *common.HTTPContext) (err error) { return func(c *common.HTTPContext) (err error) { state := c.Req().FormValue("state") nonceLock.Lock() defer nonceLock.Unlock() if _, found := nonces[state]; !found { err = fmt.Errorf("state not found") return } delete(nonces, state) scheme := "http" if c.Req().TLS != nil { scheme = "https" } redirectUrl, err := url.Parse(fmt.Sprintf("%v://%v/oauth2callback", scheme, c.Req().Host)) if err != nil { return } email, ok, err := goauth2.VerifyEmail(clientId, clientSecret, c.Req().FormValue("code"), redirectUrl) if err != nil { return } if ok { email = strings.ToLower(email) c.Session().Values[common.SessionEmail] = email u := &User{Id: kol.Id(email)} err = c.DB().Get(u) if err == kol.NotFound { err = nil u.Email = email u.Ranking = 1 } if err == nil { u.Language = common.GetLanguage(c.Req()) u.DiplicityHost = c.Req().Host u.LastLoginAt = time.Now() err = c.DB().Set(u) } } else { delete(c.Session().Values, common.SessionEmail) } c.Close() c.Resp().Header().Set("Location", "/") c.Resp().WriteHeader(302) fmt.Fprintln(c.Resp(), "/") return } }