예제 #1
0
파일: auth.go 프로젝트: hi-trust/friendfeed
func (s *Server) AuthCallback(c *gin.Context) {
	fn := gothic.GetProviderName
	gothic.GetProviderName = func(req *http.Request) (string, error) {
		provider := c.Params.ByName("provider")
		if provider == "" {
			return fn(req)
		}
		return provider, nil
	}

	provider, _ := gothic.GetProviderName(c.Request)
	u, err := gothic.CompleteUserAuth(c.Writer, c.Request)
	if err != nil {
		c.String(400, err.Error())
		return
	}

	ctx, cancel := DefaultTimeoutContext()
	defer cancel()

	authinfo := &pb.OAuthUser{
		UserId:            u.UserID,
		Name:              u.Name,
		NickName:          u.NickName,
		Email:             u.Email,
		AccessToken:       u.AccessToken,
		AccessTokenSecret: u.AccessTokenSecret,
		Provider:          provider,
	}

	profile, err := s.CurrentUser(c)
	if err != nil {
		c.Fail(500, err)
		return
	}
	authinfo.Uuid = profile.Uuid
	profile, err = s.client.PutOAuth(ctx, authinfo)
	if RequestError(c, err) {
		return
	}

	// Only allow login from google
	// Twitter only for importing feed
	if provider == "google" {
		sess := sessions.Default(c)
		sess.Set("user_id", u.UserID)
		sess.Set("uuid", profile.Uuid)
		sess.Save()
	}

	next := extractNextPath(c.Request.URL.Query().Get("state"))
	if next == "/" && provider == "twitter" {
		next = "/account/import"
	}
	http.Redirect(c.Writer, c.Request, next, http.StatusFound)
}
func callbackHandler(c *echo.Context) error {
	user, err := gothic.CompleteUserAuth(c.Response(), c.Request())
	if err != nil {
		log.Println(err)
		return err
	}
	provider, _ := gothic.GetProviderName(c.Request())
	var u *models.User
	u, err = storage.Users.Find(provider, user.UserID)
	if err != nil {
		u, err = appAuth.CreateNewOAuthUser(user)
		if err != nil {
			return err
		}
	}

	log.Println("te user : "******"oauthCallback.html", user)

	return nil
}
예제 #3
0
		BeforeEach(func() {
			ws.Auth.OAuth = OAuthConfig{
				Provider: "faux",
				Sessions: SessionsConfig{
					Type: "mock",
				},
			}
			req, err = http.NewRequest("GET", "/", nil)
			if err != nil {
				panic(err)
			}
			gothic.Store = &FakeSessionStore{}
			gothic.Store.Get(req, gothic.SessionName)
		})
		It("sets up gothic.GetProviderName to return the configured provider", func() {
			Expect(ws.Setup()).Should(Succeed())
			Expect(gothic.GetProviderName(req)).Should(Equal("faux"))
		})
		It("sets up gothic.SetState to return a unique state value every time", func() {

			Expect(ws.Setup()).Should(Succeed())
			firstState := gothic.SetState(req)
			Expect(firstState).Should(Equal(gothic.Store.(*FakeSessionStore).Session.Values["state"]))
			secondState := gothic.SetState(req)
			Expect(secondState).ShouldNot(Equal(firstState))
			Expect(secondState).Should(Equal(gothic.Store.(*FakeSessionStore).Session.Values["state"]))

		})
	})
})