woodpecker/web/login.go

159 lines
3.9 KiB
Go
Raw Normal View History

2016-03-31 06:27:53 +00:00
package web
2015-04-08 22:43:59 +00:00
import (
2015-09-30 01:21:17 +00:00
"net/http"
2015-04-13 05:32:32 +00:00
"time"
2015-04-08 22:43:59 +00:00
2015-09-30 01:21:17 +00:00
"github.com/gin-gonic/gin"
2015-04-08 22:43:59 +00:00
2015-09-30 01:21:17 +00:00
log "github.com/Sirupsen/logrus"
"github.com/drone/drone/model"
"github.com/drone/drone/remote"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/shared/crypto"
"github.com/drone/drone/shared/httputil"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/store"
2015-04-08 22:43:59 +00:00
)
func GetLogin(c *gin.Context) {
remote := remote.FromContext(c)
2015-04-08 22:43:59 +00:00
// when dealing with redirects we may need
// to adjust the content type. I cannot, however,
2016-01-10 07:00:24 +00:00
// remember why, so need to revisit this line.
2015-04-08 22:43:59 +00:00
c.Writer.Header().Del("Content-Type")
2015-09-30 01:21:17 +00:00
tmpuser, open, err := remote.Login(c.Writer, c.Request)
if err != nil {
log.Errorf("cannot authenticate user. %s", err)
c.Redirect(303, "/login?error=oauth_error")
2015-04-08 22:43:59 +00:00
return
}
2015-09-30 01:21:17 +00:00
// 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)
2015-04-08 22:43:59 +00:00
if err != nil {
count, err := store.GetUserCount(c)
if err != nil {
2015-09-30 01:21:17 +00:00
log.Errorf("cannot register %s. %s", tmpuser.Login, err)
c.Redirect(303, "/login?error=internal_error")
return
}
2015-04-08 22:43:59 +00:00
// if self-registration is disabled we should
// return a notAuthorized error. the only exception
// is if no users exist yet in the system we'll proceed.
2015-09-30 01:21:17 +00:00
if !open && count != 0 {
log.Errorf("cannot register %s. registration closed", tmpuser.Login)
c.Redirect(303, "/login?error=access_denied")
return
2015-04-08 22:43:59 +00:00
}
// create the user account
2015-09-30 01:21:17 +00:00
u = &model.User{}
u.Login = tmpuser.Login
u.Token = tmpuser.Token
u.Secret = tmpuser.Secret
u.Email = tmpuser.Email
u.Avatar = tmpuser.Avatar
u.Hash = crypto.Rand()
2015-04-08 22:43:59 +00:00
// insert the user into the database
if err := store.CreateUser(c, u); err != nil {
2015-09-30 01:21:17 +00:00
log.Errorf("cannot insert %s. %s", u.Login, err)
c.Redirect(303, "/login?error=internal_error")
2015-04-08 22:43:59 +00:00
return
}
// if this is the first user, they
// should be an admin.
if count == 0 {
2015-04-08 22:43:59 +00:00
u.Admin = true
}
}
// update the user meta data and authorization
// data and cache in the datastore.
2015-09-30 01:21:17 +00:00
u.Token = tmpuser.Token
u.Secret = tmpuser.Secret
u.Email = tmpuser.Email
u.Avatar = tmpuser.Avatar
2015-04-08 22:43:59 +00:00
if err := store.UpdateUser(c, u); err != nil {
log.Errorf("cannot update %s. %s", u.Login, err)
2015-09-30 01:21:17 +00:00
c.Redirect(303, "/login?error=internal_error")
2015-04-08 22:43:59 +00:00
return
}
exp := time.Now().Add(time.Hour * 72).Unix()
token := token.New(token.SessToken, u.Login)
tokenstr, err := token.SignExpires(u.Hash, exp)
2015-04-08 22:43:59 +00:00
if err != nil {
log.Errorf("cannot create token for %s. %s", u.Login, err)
2015-09-30 01:21:17 +00:00
c.Redirect(303, "/login?error=internal_error")
2015-04-08 22:43:59 +00:00
return
}
2015-09-30 01:21:17 +00:00
httputil.SetCookie(c.Writer, c.Request, "user_sess", tokenstr)
redirect := httputil.GetCookie(c.Request, "user_last")
if len(redirect) == 0 {
redirect = "/"
}
c.Redirect(303, redirect)
2015-04-08 22:43:59 +00:00
}
2015-09-30 01:21:17 +00:00
func GetLogout(c *gin.Context) {
httputil.DelCookie(c.Writer, c.Request, "user_sess")
httputil.DelCookie(c.Writer, c.Request, "user_last")
c.Redirect(303, "/login")
}
func GetLoginToken(c *gin.Context) {
remote := remote.FromContext(c)
2015-09-30 01:21:17 +00:00
in := &tokenPayload{}
err := c.Bind(in)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
2015-04-08 22:43:59 +00:00
return
}
2015-09-30 01:21:17 +00:00
login, err := remote.Auth(in.Access, in.Refresh)
2015-04-08 22:43:59 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusUnauthorized, err)
2015-04-08 22:43:59 +00:00
return
}
user, err := store.GetUserLogin(c, login)
2015-04-08 22:43:59 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusNotFound, err)
2015-04-08 22:43:59 +00:00
return
}
2015-09-30 01:21:17 +00:00
exp := time.Now().Add(time.Hour * 72).Unix()
token := token.New(token.SessToken, user.Login)
tokenstr, err := token.SignExpires(user.Hash, exp)
2015-04-08 22:43:59 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusInternalServerError, err)
2015-04-08 22:43:59 +00:00
return
}
2015-09-30 01:21:17 +00:00
c.IndentedJSON(http.StatusOK, &tokenPayload{
Access: tokenstr,
Expires: exp - time.Now().Unix(),
})
2015-04-08 22:43:59 +00:00
}
2015-09-30 01:21:17 +00:00
type tokenPayload struct {
Access string `json:"access_token,omitempty"`
Refresh string `json:"refresh_token,omitempty"`
Expires int64 `json:"expires_in,omitempty"`
}