2014-06-04 21:25:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
"github.com/drone/drone/plugin/remote"
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/server/database"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/server/session"
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/gorilla/pat"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoginHandler struct {
|
2014-06-12 23:41:04 +00:00
|
|
|
users database.UserManager
|
|
|
|
repos database.RepoManager
|
|
|
|
perms database.PermManager
|
2014-07-13 02:01:58 +00:00
|
|
|
//conf database.ConfigManager
|
|
|
|
sess session.Session
|
|
|
|
remotes database.RemoteManager
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
func NewLoginHandler(users database.UserManager, repos database.RepoManager, perms database.PermManager, sess session.Session /*conf database.ConfigManager,*/, remotes database.RemoteManager) *LoginHandler {
|
|
|
|
return &LoginHandler{users, repos, perms /*conf,*/, sess, remotes}
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLogin gets the login to the 3rd party remote system.
|
|
|
|
// GET /login/:host
|
|
|
|
func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error {
|
2014-09-02 07:18:17 +00:00
|
|
|
var host = r.FormValue(":host")
|
|
|
|
var redirect = "/"
|
|
|
|
var remote = remote.Lookup(host)
|
|
|
|
if remote == nil {
|
2014-06-04 21:25:38 +00:00
|
|
|
return notFound{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// authenticate the user
|
2014-09-02 07:18:17 +00:00
|
|
|
login, err := remote.Authorize(w, r)
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return badRequest{err}
|
|
|
|
} else if login == nil {
|
|
|
|
// in this case we probably just redirected
|
|
|
|
// the user, so we can exit with no error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the user from the database
|
|
|
|
u, err := h.users.FindLogin(host, login.Login)
|
|
|
|
if err != nil {
|
2014-06-09 21:32:00 +00:00
|
|
|
// if self-registration is disabled we should
|
2014-06-21 21:22:38 +00:00
|
|
|
// return a notAuthorized error. the only exception
|
|
|
|
// is if no users exist yet in the system we'll proceed.
|
2014-09-02 07:18:17 +00:00
|
|
|
if h.users.Exist() {
|
2014-06-09 21:32:00 +00:00
|
|
|
return notAuthorized{}
|
|
|
|
}
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
// create the user account
|
2014-09-02 07:18:17 +00:00
|
|
|
u = model.NewUser(remote.GetKind(), login.Login, login.Email)
|
2014-06-04 21:25:38 +00:00
|
|
|
u.Name = login.Name
|
|
|
|
u.SetEmail(login.Email)
|
|
|
|
|
|
|
|
// insert the user into the database
|
|
|
|
if err := h.users.Insert(u); err != nil {
|
|
|
|
return badRequest{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this is the first user, they
|
|
|
|
// should be an admin.
|
|
|
|
if u.ID == 1 {
|
|
|
|
u.Admin = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the user access token
|
|
|
|
// in case it changed in GitHub
|
|
|
|
u.Access = login.Access
|
|
|
|
u.Secret = login.Secret
|
2014-07-10 01:39:31 +00:00
|
|
|
u.Name = login.Name
|
2014-07-09 21:25:11 +00:00
|
|
|
u.SetEmail(login.Email)
|
2014-08-31 17:18:09 +00:00
|
|
|
u.Syncing = true //u.IsStale() // todo (badrydzewski) should not always sync
|
2014-06-04 21:25:38 +00:00
|
|
|
if err := h.users.Update(u); err != nil {
|
|
|
|
return badRequest{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// look at the last synchronized date to determine if
|
|
|
|
// we need to re-sync the account.
|
2014-06-05 22:04:59 +00:00
|
|
|
//
|
2014-07-11 20:45:32 +00:00
|
|
|
// todo(bradrydzewski) this should move to a server/sync package and
|
|
|
|
// should be injected into this struct, just like the database code.
|
|
|
|
//
|
|
|
|
// todo(bradrydzewski) this login should be a bit more intelligent
|
|
|
|
// than the current implementation.
|
|
|
|
//
|
|
|
|
// todo(bradrydzewski) the github implementation will only sync a
|
|
|
|
// maximum of 100 repositories due to the api pagination. need to fix.
|
|
|
|
if u.Syncing {
|
2014-07-10 05:24:06 +00:00
|
|
|
redirect = "/sync"
|
2014-06-04 21:25:38 +00:00
|
|
|
log.Println("sync user account.", u.Login)
|
|
|
|
|
|
|
|
// sync inside a goroutine. This should eventually be moved to
|
|
|
|
// its own package / sync utility.
|
|
|
|
go func() {
|
2014-09-02 07:18:17 +00:00
|
|
|
repos, err := remote.GetRepos(u)
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error syncing user account, listing repositories", u.Login, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert all repositories
|
2014-09-02 07:18:17 +00:00
|
|
|
for _, repo := range repos {
|
|
|
|
var role = repo.Role
|
2014-06-04 21:25:38 +00:00
|
|
|
if err := h.repos.Insert(repo); err != nil {
|
2014-08-12 06:15:38 +00:00
|
|
|
// typically we see a failure because the repository already exists
|
|
|
|
// in which case, we can retrieve the existing record to get the ID.
|
|
|
|
repo, err = h.repos.FindName(repo.Host, repo.Owner, repo.Name)
|
|
|
|
if err != nil {
|
2014-09-02 07:18:17 +00:00
|
|
|
log.Println("Error adding repo.", u.Login, repo.Name, err)
|
2014-08-12 06:15:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add user permissions
|
2014-09-02 07:18:17 +00:00
|
|
|
if err := h.perms.Grant(u, repo, role.Read, role.Write, role.Admin); err != nil {
|
|
|
|
log.Println("Error adding permissions.", u.Login, repo.Name, err)
|
2014-06-04 21:25:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
log.Println("Successfully syced repo.", u.Login+"/"+repo.Name)
|
2014-07-13 02:01:58 +00:00
|
|
|
}
|
2014-07-11 20:45:32 +00:00
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
u.Synced = time.Now().UTC().Unix()
|
|
|
|
u.Syncing = false
|
|
|
|
if err := h.users.Update(u); err != nil {
|
|
|
|
log.Println("Error syncing user account, updating sync date", u.Login, err)
|
|
|
|
return
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// (re)-create the user session
|
|
|
|
h.sess.SetUser(w, r, u)
|
|
|
|
|
|
|
|
// redirect the user to their dashboard
|
2014-07-10 05:24:06 +00:00
|
|
|
http.Redirect(w, r, redirect, http.StatusSeeOther)
|
2014-06-04 21:25:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLogout terminates the current user session
|
|
|
|
// GET /logout
|
|
|
|
func (h *LoginHandler) GetLogout(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
h.sess.Clear(w, r)
|
2014-07-11 20:45:32 +00:00
|
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
2014-06-04 21:25:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *LoginHandler) Register(r *pat.Router) {
|
|
|
|
r.Get("/login/{host}", errorHandler(h.GetLogin))
|
2014-09-03 08:12:03 +00:00
|
|
|
r.Post("/login/{host}", errorHandler(h.GetLogin))
|
2014-06-04 21:25:38 +00:00
|
|
|
r.Get("/logout", errorHandler(h.GetLogout))
|
|
|
|
}
|