// NewServerHelper creates a new server helper object. // Titan server instance is initialized and ready to accept connection after this function return. func NewServerHelper(t *testing.T) *ServerHelper { if testing.Short() { t.Skip("Skipping integration test in short testing mode") } if (titan.Conf == titan.Config{}) { titan.InitConf("test") } url := "127.0.0.1:" + titan.Conf.App.Port s, err := titan.NewServer(url) if err != nil { t.Fatal("Failed to create server:", err) } var db data.DB if *awsFlag { db = aws.NewDynamoDB("", "") if err := db.Seed(true, titan.Conf.App.JWTPass()); err != nil { t.Fatal("Failed to set seed DynamoDB:", err) } } else { db = inmem.NewDB() } if err := s.SetDB(db); err != nil { t.Fatal("Failed to set server database instance:", err) } h := ServerHelper{ db: db, server: s, testing: t, serverClosed: make(chan bool), } return &h }
// googleAuth authenticates a user with Google+ using provided OAuth 2.0 access token. // If authenticated successfully, user profile is retrieved from Google+ and user is given a JWT token in return. func googleAuth(ctx *neptulon.ReqCtx, db data.DB, pass string) error { var r tokenContainer if err := ctx.Params(&r); err != nil || r.Token == "" { ctx.Err = &neptulon.ResError{Code: 666, Message: "Malformed or null Google oauth access token was provided."} return fmt.Errorf("auth: google: malformed or null Google oauth token '%v' was provided: %v", r.Token, err) } p, err := getTokenInfo(r.Token) if err != nil { ctx.Err = &neptulon.ResError{Code: 666, Message: "Failed to authenticated with the given Google oauth access token."} return fmt.Errorf("auth: google: error during Google API call using provided token: %v with error: %v", r.Token, err) } // retrieve user information user, ok := db.GetByEmail(p.Email) if !ok { // this is a first-time registration so create user profile via Google+ profile info user = &models.User{Email: p.Email, Name: p.Name, Picture: p.Picture, Registered: time.Now()} // save the user information for user ID to be generated by the database if ierr := db.SaveUser(user); ierr != nil { return fmt.Errorf("auth: google: failed to persist user information: %v", ierr) } // create the JWT token token := jwt.New(jwt.SigningMethodHS256) token.Claims["userid"] = user.ID token.Claims["created"] = user.Registered.Unix() user.JWTToken, err = token.SignedString([]byte(pass)) if err != nil { return fmt.Errorf("auth: google: jwt signing error: %v", err) } // now save the full user info if err := db.SaveUser(user); err != nil { return fmt.Errorf("auth: google: failed to persist user information: %v", err) } // store user ID in session so user can make authenticated call after this ctx.Conn.Session.Set("userid", user.ID) } ctx.Res = gAuthRes{ID: user.ID, Token: user.JWTToken, Name: user.Name, Email: user.Email, Picture: user.Picture} ctx.Session.Set(middleware.CustResLogDataKey, gAuthRes{ID: user.ID, Token: user.JWTToken, Name: user.Name, Email: user.Email}) log.Printf("auth: google: logged in: %v, %v", p.Name, p.Email) return nil }