Example #1
0
func Index(c *gin.Context) {
	user := session.User(c)

	switch {
	case user == nil:
		c.HTML(200, "brand.html", gin.H{})
	default:
		teams, _ := cache.GetTeams(c, user)
		csrf, _ := token.New(token.CsrfToken, user.Login).Sign(user.Secret)
		c.HTML(200, "index.html", gin.H{"user": user, "csrf": csrf, "teams": teams})
	}
}
Example #2
0
// PostRepo activates a new repository.
func PostRepo(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		user  = session.User(c)
	)

	// verify repo doesn't already exist
	if _, err := store.GetRepoOwnerName(c, owner, name); err == nil {
		c.AbortWithStatus(409)
		c.String(409, "Error activating a repository that is already active.")
		return
	}

	repo, err := remote.GetRepo(c, user, owner, name)
	if err != nil {
		c.String(404, "Error finding repository in GitHub. %s")
		return
	}
	repo.UserID = user.ID
	repo.Secret = model.Rand()

	// creates a token to authorize the link callback url
	t := token.New(token.HookToken, repo.Slug)
	sig, err := t.Sign(repo.Secret)
	if err != nil {
		c.String(500, "Error activating repository. %s")
		return
	}

	// create the hook callback url
	link := fmt.Sprintf(
		"%s/hook?access_token=%s",
		httputil.GetURL(c.Request),
		sig,
	)
	err = remote.SetHook(c, user, repo, link)
	if err != nil {
		c.String(500, "Error creating hook. %s", err)
		return
	}

	err = store.CreateRepo(c, repo)
	if err != nil {
		c.String(500, "Error activating the repository. %s", err)
		return
	}
	c.JSON(200, repo)
}
Example #3
0
File: login.go Project: lgtmco/lgtm
// LoginToken authenticates a user with their GitHub token and
// returns an LGTM API token in the response.
func LoginToken(c *gin.Context) {
	access := c.Query("access_token")
	login, err := remote.GetUserToken(c, access)
	if err != nil {
		c.String(403, "Unable to authenticate user. %s", err)
		return
	}
	user, err := store.GetUserLogin(c, login)
	if err != nil {
		c.String(404, "Unable to authenticate user %s. Not registered.", user.Login)
		return
	}
	exp := time.Now().Add(time.Hour * 72).Unix()
	token := token.New(token.UserToken, user.Login)
	tokenstr, err := token.SignExpires(user.Secret, exp)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}
	c.IndentedJSON(http.StatusOK, &tokenPayload{
		Access:  tokenstr,
		Expires: exp - time.Now().Unix(),
	})
}
Example #4
0
File: login.go Project: lgtmco/lgtm
// Login attempts to authorize a user via GitHub oauth2. If the user does not
// yet exist, and new account is created. Upon successful login the user is
// redirected to the main screen.
func Login(c *gin.Context) {
	// render the error page if the login fails. Without this block
	// we would encounter an infinite number of redirects.
	if err := c.Query("error"); len(err) != 0 {
		c.HTML(500, "error.html", gin.H{"error": err})
		return
	}

	// when dealing with redirects we may need
	// to adjust the content type. I cannot, however,
	// rememver why, so need to revisit this line.
	c.Writer.Header().Del("Content-Type")

	tmpuser, err := remote.GetUser(c, c.Writer, c.Request)
	if err != nil {
		log.Errorf("cannot authenticate user. %s", err)
		c.Redirect(303, "/login?error=oauth_error")
		return
	}
	// this will happen when the user is redirected by
	// the remote provide as part of the oauth dance.
	if tmpuser == nil {
		return
	}

	// get the user from the database
	u, err := store.GetUserLogin(c, tmpuser.Login)
	if err != nil {

		// create the user account
		u = &model.User{}
		u.Login = tmpuser.Login
		u.Token = tmpuser.Token
		u.Avatar = tmpuser.Avatar
		u.Secret = model.Rand()

		// insert the user into the database
		if err := store.CreateUser(c, u); err != nil {
			log.Errorf("cannot insert %s. %s", u.Login, err)
			c.Redirect(303, "/login?error=internal_error")
			return
		}
	}

	// update the user meta data and authorization
	// data and cache in the datastore.
	u.Token = tmpuser.Token
	u.Avatar = tmpuser.Avatar

	if err := store.UpdateUser(c, u); err != nil {
		log.Errorf("cannot update %s. %s", u.Login, err)
		c.Redirect(303, "/login?error=internal_error")
		return
	}

	exp := time.Now().Add(time.Hour * 72).Unix()
	token := token.New(token.SessToken, u.Login)
	tokenstr, err := token.SignExpires(u.Secret, exp)
	if err != nil {
		log.Errorf("cannot create token for %s. %s", u.Login, err)
		c.Redirect(303, "/login?error=internal_error")
		return
	}

	httputil.SetCookie(c.Writer, c.Request, "user_sess", tokenstr)
	c.Redirect(303, "/")
}