2016-05-24 11:55:48 +02:00
|
|
|
package api
|
2016-01-05 00:32:53 +01:00
|
|
|
|
|
|
|
import (
|
2016-04-30 14:28:47 +02:00
|
|
|
"database/sql"
|
2016-01-05 00:32:53 +01:00
|
|
|
"fmt"
|
2017-02-23 00:21:49 +01:00
|
|
|
"net/http"
|
2016-04-09 21:09:57 +02:00
|
|
|
"strings"
|
2016-01-05 00:32:53 +01:00
|
|
|
"time"
|
2016-03-16 22:49:43 +01:00
|
|
|
|
2017-02-23 06:12:16 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2016-03-16 22:49:43 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2016-01-05 00:32:53 +01:00
|
|
|
)
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
//nolint: gocyclo
|
2019-07-09 09:21:49 +02:00
|
|
|
func authentication(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var userID int
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
if authHeader := strings.ToLower(r.Header.Get("authorization")); len(authHeader) > 0 && strings.Contains(authHeader, "bearer") {
|
|
|
|
var token db.APIToken
|
|
|
|
if err := db.Mysql.SelectOne(&token, "select * from user__token where id=? and expired=0", strings.Replace(authHeader, "bearer ", "", 1)); err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userID = token.UserID
|
|
|
|
} else {
|
|
|
|
// fetch session from cookie
|
|
|
|
cookie, err := r.Cookie("semaphore")
|
|
|
|
if err != nil {
|
2018-10-22 09:19:33 +02:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2016-04-30 14:28:47 +02:00
|
|
|
return
|
2016-04-09 21:09:57 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
value := make(map[string]interface{})
|
|
|
|
if err = util.Cookie.Decode("semaphore", cookie.Value, &value); err != nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
user, ok := value["user"]
|
|
|
|
sessionVal, okSession := value["session"]
|
|
|
|
if !ok || !okSession {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
userID = user.(int)
|
|
|
|
sessionID := sessionVal.(int)
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
// fetch session
|
|
|
|
var session db.Session
|
|
|
|
if err := db.Mysql.SelectOne(&session, "select * from session where id=? and user_id=? and expired=0", sessionID, userID); err != nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
if time.Since(session.LastActive).Hours() > 7*24 {
|
|
|
|
// more than week old unused session
|
|
|
|
// destroy.
|
|
|
|
if _, err := db.Mysql.Exec("update session set expired=1 where id=?", sessionID); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
if _, err := db.Mysql.Exec("update session set last_active=UTC_TIMESTAMP() where id=?", sessionID); err != nil {
|
2016-04-30 14:28:47 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-07-09 09:21:49 +02:00
|
|
|
}
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
user, err := db.FetchUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Can't find user", err)
|
2018-10-22 09:19:33 +02:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2016-04-30 14:28:47 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
context.Set(r, "user", user)
|
2019-07-09 18:11:01 +02:00
|
|
|
next.ServeHTTP(w, r)
|
2019-07-09 09:21:49 +02:00
|
|
|
})
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|