Ejemplo n.º 1
0
// Function that handles the callback from the Google server
func (this *FacebookController) AuthCallback(ctx framework.WebContext) framework.WebResult {
	//Get the code from the response
	code, _ := ctx.Param("code")

	t := &oauth.Transport{Config: this.oauthCfg}

	// Exchange the received code for a token
	t.Exchange(code)

	//now get user data based on the Transport which has the token
	resp, err := t.Client().Get(this.profileInfoURL)

	if err != nil {
		log.Println(err)
		return ctx.FrameworkError(framework.ToError(framework.Error_Web_UnableToAuthenticate, err))
	}

	userprofile := make(map[string]interface{})
	decoder := json.NewDecoder(resp.Body)
	err = decoder.Decode(&userprofile)

	if err != nil {
		log.Println(err)
		return ctx.FrameworkError(framework.ToError(framework.Error_Web_UnableToAuthenticate, err))
	}

	fmt.Println(userprofile)

	email := userprofile["email"].(string)
	name := userprofile["name"].(string)

	userDto := &contracts.User{Email: email, Name: name}
	userCredentialsDto := &contracts.UserAuthCredentials{
		Type:         framework.AuthType_Facebook,
		AccessToken:  t.Token.AccessToken,
		RefreshToken: t.Token.RefreshToken,
		Expiry:       t.Token.Expiry,
	}

	user, err2 := this.userService.SaveWithCredentials(userDto, userCredentialsDto)

	if err2 != nil {
		return ctx.Error(err2)
	}

	ctx.Session().SetUser(&framework.SessionUser{
		UserId:   user.Email,
		AuthType: framework.AuthType_Facebook,
		AuthData: t.AccessToken,
	})

	return ctx.Template(userInfoTemplate, fmt.Sprintf("Name: %s Email: %s", name, email))
}
Ejemplo n.º 2
0
func getFrameworkError(err error) *framework.FrameworkError {

	//err.(type) gets the type of err and can only be used in a switch (aka type switch)
	switch err.(type) {

	case *mgo.LastError:
		return fromMongoError(err.(*mgo.LastError))
	default:
		return framework.ToError(framework.Error_Generic, err)
	}
}
Ejemplo n.º 3
0
func fromMongoError(err *mgo.LastError) *framework.FrameworkError {

	var errorCode framework.FrameworkErrorCode

	switch err.Code {
	case 11000:
		errorCode = framework.Error_Db_DuplicateId
	default:
		errorCode = framework.Error_Generic
	}

	return framework.ToError(errorCode, err)
}
Ejemplo n.º 4
0
func (this *webServer) processError(context *webContext, err error) {
	var fErr *framework.FrameworkError

	switch err.(type) {

	case *framework.FrameworkError:
		fErr = err.(*framework.FrameworkError)
	default:
		fErr = framework.ToError(framework.Error_Generic, err)
	}

	log.Println(fmt.Sprintf("Aborting due to %s", fErr.ErrorMessage))
	context.Abort(400, fErr.ErrorMessage)
}