set ldap auth
This commit is contained in:
parent
0deab5858d
commit
ac4055cf33
|
@ -0,0 +1 @@
|
||||||
|
foods.db
|
|
@ -0,0 +1,38 @@
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/jtblin/go-ldap-client"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogIn auth the client
|
||||||
|
func LogIn(username string, password string) (ok bool, user map[string]string) {
|
||||||
|
|
||||||
|
ldapclient := &ldap.LDAPClient{
|
||||||
|
Base: "o=gouv,c=fr",
|
||||||
|
Host: "ldap.cadoles.com",
|
||||||
|
Port: 389,
|
||||||
|
UseSSL: false,
|
||||||
|
BindDN: "cn=reader,o=gouv,c=fr",
|
||||||
|
BindPassword: "ohc7kei8lil8Zoesai5chisaiGhu5Yaisai6kaegh9aingai0pae8ohb",
|
||||||
|
UserFilter: "(uid=%s)",
|
||||||
|
GroupFilter: "(memberUid=%s)",
|
||||||
|
Attributes: []string{"uid", "mail"},
|
||||||
|
}
|
||||||
|
defer ldapclient.Close()
|
||||||
|
|
||||||
|
ok, user, err := ldapclient.Authenticate(username, password)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error authenticating user %s: %+v", "username", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
log.Printf("Authenticating failed for user %s", "username")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("User %s authentificated", username)
|
||||||
|
return ok, user
|
||||||
|
|
||||||
|
}
|
32
bdd/bdd.go
32
bdd/bdd.go
|
@ -3,6 +3,7 @@ package bdd
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
|
@ -37,6 +38,16 @@ func CloseDB(db *bolt.DB) error {
|
||||||
return db.Close()
|
return db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InitDB init the db
|
||||||
|
func InitDB() {
|
||||||
|
db, err := OpenDB()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("\nOpenDB error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
CloseDB(db)
|
||||||
|
}
|
||||||
|
|
||||||
// AddVote ajoute un vote à la bdd
|
// AddVote ajoute un vote à la bdd
|
||||||
func AddVote(db *bolt.DB, vote string, date time.Time) error {
|
func AddVote(db *bolt.DB, vote string, date time.Time) error {
|
||||||
|
|
||||||
|
@ -72,9 +83,28 @@ func GetVotesOfTheDay(db *bolt.DB, date time.Time) ([]string, error) {
|
||||||
err := db.View(func(tx *bolt.Tx) error {
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor()
|
c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor()
|
||||||
min := []byte(time.Now().AddDate(0, 0, -1).Format(time.RFC3339))
|
min := []byte(time.Now().AddDate(0, 0, -1).Format(time.RFC3339))
|
||||||
max := []byte(time.Now().AddDate(0, 0, 0).Format(time.RFC3339))
|
max := []byte(date.Format(time.RFC3339))
|
||||||
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
|
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
|
||||||
res = append(res, string(v))
|
res = append(res, string(v))
|
||||||
|
fmt.Println(string(k))
|
||||||
|
fmt.Println(string(v))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVotesOfTheRange liste tous les votes du jour
|
||||||
|
func GetVotesOfTheRange(db *bolt.DB, min time.Time, max time.Time) ([]string, error) {
|
||||||
|
res := []string{}
|
||||||
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
|
c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor()
|
||||||
|
mino := []byte(min.Format(time.RFC3339))
|
||||||
|
maxo := []byte(max.Format(time.RFC3339))
|
||||||
|
for k, v := c.Seek(mino); k != nil && bytes.Compare(k, maxo) <= 0; k, v = c.Next() {
|
||||||
|
res = append(res, string(v))
|
||||||
|
fmt.Println(string(k))
|
||||||
|
fmt.Println(string(v))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
92
server.go
92
server.go
|
@ -9,36 +9,78 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// User is a user
|
||||||
|
type User struct {
|
||||||
|
Name string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
http.HandleFunc("/", Base)
|
bdd.InitDB()
|
||||||
http.HandleFunc("/results", ResultsPage)
|
|
||||||
db, err := bdd.OpenDB()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("\nOpenDB error: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
bdd.CloseDB(db)
|
|
||||||
|
|
||||||
vote.GetVotesOfTheDay()
|
vote.GetVotesOfTheDay()
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.Handle("/", &User{})
|
||||||
|
s := &http.Server{Addr: "localhost:8080", Handler: mux}
|
||||||
|
|
||||||
log.Print("\nready: listening on localhost:8080\n")
|
log.Print("\nready: listening on localhost:8080\n")
|
||||||
|
|
||||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
if err := s.ListenAndServe(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Base is the entry point to all requests
|
// ServerHTTP is the entry point to all requests
|
||||||
func Base(w http.ResponseWriter, r *http.Request) {
|
func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
if r.Method == http.MethodPost {
|
log.Printf("ParseForm() err: %v", err)
|
||||||
VotePage(w, r)
|
|
||||||
} else if r.Method == http.MethodGet {
|
|
||||||
HomePage(w, r)
|
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
u.Auth(r.FormValue("user"), r.FormValue("password"))
|
||||||
w.Write([]byte("400 - Unsupported Request Method!"))
|
}
|
||||||
|
if u.Name == "" {
|
||||||
|
LogInPage(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r.Method == http.MethodPost && r.Form.Get("option") != "" {
|
||||||
|
VoteEndPoint(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r.URL.Path == "/" {
|
||||||
|
HomePage(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r.URL.Path == "/results" {
|
||||||
|
ResultsPage(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auth is the login page for user
|
||||||
|
func (u *User) Auth(user string, pass string) bool {
|
||||||
|
// auth, _ := auth.LogIn(user, pass)
|
||||||
|
// if auth {
|
||||||
|
// u.Name = user
|
||||||
|
// u.Password = pass
|
||||||
|
// }
|
||||||
|
u.Name = "mlamalle"
|
||||||
|
u.Password = "password"
|
||||||
|
var auth = true
|
||||||
|
return auth
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogInPage is the log in page of the app
|
||||||
|
func LogInPage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
paths := []string{
|
||||||
|
"./templates/login.tmpl",
|
||||||
|
}
|
||||||
|
t := template.Must(template.New("login.tmpl").ParseFiles(paths...))
|
||||||
|
err := t.Execute(w, "")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("\nExecute error: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,26 +100,20 @@ func HomePage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// ResultsPage is the page displaiyng the votes results
|
// ResultsPage is the page displaiyng the votes results
|
||||||
func ResultsPage(w http.ResponseWriter, r *http.Request) {
|
func ResultsPage(w http.ResponseWriter, r *http.Request) {
|
||||||
db, err := bdd.OpenDB()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("\nOpenDB error: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//vote, _ := bdd.GetVotesOfTheDay(db, time.Now().AddDate(0, 0, -1))
|
|
||||||
bdd.CloseDB(db)
|
|
||||||
paths := []string{
|
paths := []string{
|
||||||
"./templates/results.tmpl",
|
"./templates/results.tmpl",
|
||||||
}
|
}
|
||||||
t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
|
t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
|
||||||
err = t.Execute(w, "")
|
err := t.Execute(w, vote.GetVotesOfTheDay())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("\nExecute error: %v", err)
|
log.Printf("\nExecute error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// VotePage is the endpoint to add votes
|
// VoteEndPoint is the endpoint to add votes
|
||||||
func VotePage(w http.ResponseWriter, r *http.Request) {
|
func VoteEndPoint(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
option := r.Form.Get("option")
|
option := r.Form.Get("option")
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Foodoles written in Go</title>
|
||||||
|
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.css">
|
||||||
|
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
|
||||||
|
<style type="text/css"> h2 { margin: 2em 0em; } .ui.container { padding-top: 5em; padding-bottom: 5em; } </style>
|
||||||
|
|
||||||
|
<style type="text/css">
|
||||||
|
body > .grid {
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
.image {
|
||||||
|
margin-top: -100px;
|
||||||
|
}
|
||||||
|
.column {
|
||||||
|
max-width: 450px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
$(document)
|
||||||
|
.ready(function() {
|
||||||
|
$('.ui.form')
|
||||||
|
.form({
|
||||||
|
fields: {
|
||||||
|
password: {
|
||||||
|
identifier : 'password',
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
type : 'empty',
|
||||||
|
prompt : 'Please enter your password'
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
;
|
||||||
|
})
|
||||||
|
;
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="ui middle aligned center aligned grid">
|
||||||
|
<div class="column">
|
||||||
|
<h2 class="ui blue image header">
|
||||||
|
<div class="content">
|
||||||
|
Log-in
|
||||||
|
</div>
|
||||||
|
</h2>
|
||||||
|
<form class="ui large form" action="/" method="POST">
|
||||||
|
<div class="ui stacked segment">
|
||||||
|
<div class="field">
|
||||||
|
<div class="ui left icon input">
|
||||||
|
<i class="user icon"></i>
|
||||||
|
<input type="text" name="user" placeholder="User Name">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<div class="ui left icon input">
|
||||||
|
<i class="lock icon"></i>
|
||||||
|
<input type="password" name="password" placeholder="Password">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="ui fluid large blue submit button">Login</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="ui error message"></div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
|
@ -5,25 +5,20 @@
|
||||||
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
|
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
|
||||||
<style type="text/css"> h2 { margin: 2em 0em; } .ui.container { padding-top: 5em; padding-bottom: 5em; } </style>
|
<style type="text/css"> h2 { margin: 2em 0em; } .ui.container { padding-top: 5em; padding-bottom: 5em; } </style>
|
||||||
<script src="https://js.pusher.com/4.1/pusher.min.js"></script>
|
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.js"></script>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{{ range .Foods }}
|
|
||||||
<div>
|
|
||||||
<div class="ui massive label"> {{ .Icon }}</div>
|
|
||||||
</div>
|
|
||||||
{{ end }}
|
|
||||||
<div class="ui container">
|
<div class="ui container">
|
||||||
<h2 class="ui header">TV Show - Poll Results</h2><br/>
|
<h2 class="ui header">Foodoles - Results</h2><br/>
|
||||||
<div class="ui one column grid link cards">
|
<div class="ui one column grid link cards">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div id="canvas" style="height: 500px; width: 100%;"></div>
|
{{ range .Votes }}
|
||||||
|
{{.}}
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="color:#fff" id="key">{{.Key}}</div>
|
|
||||||
<div style="color:#fff" id="cluster">{{.Cluster}}</div>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -33,9 +33,8 @@ func ForFood(Food string) {
|
||||||
|
|
||||||
// GetVotesOfTheDay récupère les votes du jour
|
// GetVotesOfTheDay récupère les votes du jour
|
||||||
func GetVotesOfTheDay() VotesOfTheDay {
|
func GetVotesOfTheDay() VotesOfTheDay {
|
||||||
time.Now()
|
|
||||||
vo := VotesOfTheDay{time.Now().Format("02/01/2006"), nil}
|
vo := VotesOfTheDay{time.Now().Format("02/01/2006"), nil}
|
||||||
duplicate := FoodList()
|
duplicate := FoodVotesList()
|
||||||
|
|
||||||
dupmap := dupcount(duplicate)
|
dupmap := dupcount(duplicate)
|
||||||
|
|
||||||
|
@ -49,8 +48,8 @@ func GetVotesOfTheDay() VotesOfTheDay {
|
||||||
return vo
|
return vo
|
||||||
}
|
}
|
||||||
|
|
||||||
// FoodList return a list of food
|
// FoodVotesList return a list of votes for food
|
||||||
func FoodList() []Vote {
|
func FoodVotesList() []Vote {
|
||||||
f := []Vote{}
|
f := []Vote{}
|
||||||
|
|
||||||
db, err := bdd.OpenDB()
|
db, err := bdd.OpenDB()
|
||||||
|
@ -58,7 +57,7 @@ func FoodList() []Vote {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lvotes, _ := bdd.GetVotesOfTheDay(db, time.Now())
|
lvotes, _ := bdd.GetVotesOfTheRange(db, time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 0))
|
||||||
|
|
||||||
for _, fo := range lvotes {
|
for _, fo := range lvotes {
|
||||||
vf := Vote{fo, 1}
|
vf := Vote{fo, 1}
|
||||||
|
|
Loading…
Reference in New Issue