set ldap auth

This commit is contained in:
Matthieu Lamalle 2019-11-27 10:06:24 +01:00
parent 0deab5858d
commit ac4055cf33
8 changed files with 225 additions and 46 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
foods.db

38
auth/auth.go Normal file
View File

@ -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
}

View File

@ -3,6 +3,7 @@ package bdd
import (
"bytes"
"fmt"
"log"
"time"
"github.com/boltdb/bolt"
@ -37,6 +38,16 @@ func CloseDB(db *bolt.DB) error {
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
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 {
c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor()
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() {
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
})

BIN
server

Binary file not shown.

View File

@ -9,36 +9,78 @@ import (
"net/http"
)
// User is a user
type User struct {
Name string
Password string
}
func main() {
http.HandleFunc("/", Base)
http.HandleFunc("/results", ResultsPage)
db, err := bdd.OpenDB()
if err != nil {
log.Printf("\nOpenDB error: %v", err)
return
}
bdd.CloseDB(db)
bdd.InitDB()
vote.GetVotesOfTheDay()
mux := http.NewServeMux()
mux.Handle("/", &User{})
s := &http.Server{Addr: "localhost:8080", Handler: mux}
log.Print("\nready: listening on localhost:8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
if err := s.ListenAndServe(); err != nil {
panic(err)
}
}
// Base is the entry point to all requests
func Base(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
VotePage(w, r)
} else if r.Method == http.MethodGet {
HomePage(w, r)
// ServerHTTP is the entry point to all requests
func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
log.Printf("ParseForm() err: %v", err)
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Unsupported Request Method!"))
u.Auth(r.FormValue("user"), r.FormValue("password"))
}
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
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{
"./templates/results.tmpl",
}
t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
err = t.Execute(w, "")
err := t.Execute(w, vote.GetVotesOfTheDay())
if err != nil {
log.Printf("\nExecute error: %v", err)
return
}
}
// VotePage is the endpoint to add votes
func VotePage(w http.ResponseWriter, r *http.Request) {
// VoteEndPoint is the endpoint to add votes
func VoteEndPoint(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
option := r.Form.Get("option")

80
templates/login.tmpl Normal file
View File

@ -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>

View File

@ -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://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>
<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>
<body>
{{ range .Foods }}
<div>
<div class="ui massive label"> {{ .Icon }}</div>
</div>
{{ end }}
<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="card">
<div id="canvas" style="height: 500px; width: 100%;"></div>
{{ range .Votes }}
{{.}}
{{ end }}
</div>
</div>
</div>
<div style="color:#fff" id="key">{{.Key}}</div>
<div style="color:#fff" id="cluster">{{.Cluster}}</div>
</body>
</html>

View File

@ -33,9 +33,8 @@ func ForFood(Food string) {
// GetVotesOfTheDay récupère les votes du jour
func GetVotesOfTheDay() VotesOfTheDay {
time.Now()
vo := VotesOfTheDay{time.Now().Format("02/01/2006"), nil}
duplicate := FoodList()
duplicate := FoodVotesList()
dupmap := dupcount(duplicate)
@ -49,8 +48,8 @@ func GetVotesOfTheDay() VotesOfTheDay {
return vo
}
// FoodList return a list of food
func FoodList() []Vote {
// FoodVotesList return a list of votes for food
func FoodVotesList() []Vote {
f := []Vote{}
db, err := bdd.OpenDB()
@ -58,7 +57,7 @@ func FoodList() []Vote {
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 {
vf := Vote{fo, 1}