Added logout and some logging
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
var ErrInvalidSession = fmt.Errorf(`invalid session token`)
|
||||||
|
|
||||||
type User interface {
|
type User interface {
|
||||||
GetUsername() string
|
GetUsername() string
|
||||||
GetName() string
|
GetName() string
|
||||||
|
|||||||
+9
-1
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
@@ -59,15 +60,22 @@ func (a *LDAPAuthService) doGetRequest(url string, response interface{}) error {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf(`GET %q resulted in %v`, url, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf(`GET %q resulted in %v`, url, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.NewDecoder(res.Body).Decode(response)
|
if err := json.NewDecoder(res.Body).Decode(response); err != nil {
|
||||||
|
log.Printf(`GET %q resulted in %v`, url, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *LDAPAuthService) doPostRequest(url string, request interface{}, response interface{}) error {
|
func (a *LDAPAuthService) doPostRequest(url string, request interface{}, response interface{}) error {
|
||||||
|
|||||||
+1
-1
@@ -89,7 +89,7 @@ func (m *Memory) GetUsers() ([]User, error) {
|
|||||||
func (m *Memory) GetSession(token string) (Session, error) {
|
func (m *Memory) GetSession(token string) (Session, error) {
|
||||||
session, ok := m.Sessions[token]
|
session, ok := m.Sessions[token]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf(`invalid session token`)
|
return nil, ErrInvalidSession
|
||||||
}
|
}
|
||||||
|
|
||||||
return session, nil
|
return session, nil
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ func main() {
|
|||||||
app.Get("/profilo", func(c *fiber.Ctx) error {
|
app.Get("/profilo", func(c *fiber.Ctx) error {
|
||||||
user, ok := c.Locals("user").(auth.User)
|
user, ok := c.Locals("user").(auth.User)
|
||||||
if !ok || user == nil {
|
if !ok || user == nil {
|
||||||
return fmt.Errorf(`no logged in user`)
|
return fmt.Errorf(`user not logged in`)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Type("html")
|
c.Type("html")
|
||||||
@@ -156,6 +156,16 @@ func main() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.Get("/logout", func(c *fiber.Ctx) error {
|
||||||
|
c.Cookie(&fiber.Cookie{
|
||||||
|
Name: "session-token",
|
||||||
|
Path: "/",
|
||||||
|
Value: "",
|
||||||
|
Expires: time.Now(),
|
||||||
|
})
|
||||||
|
return c.Redirect("/")
|
||||||
|
})
|
||||||
|
|
||||||
app.Get("/news/:article", func(c *fiber.Ctx) error {
|
app.Get("/news/:article", func(c *fiber.Ctx) error {
|
||||||
articleID := c.Params("article")
|
articleID := c.Params("article")
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
<p>
|
<p>
|
||||||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quae earum amet delectus cumque obcaecati minus quos aliquid fugiat reprehenderit voluptatum?
|
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quae earum amet delectus cumque obcaecati minus quos aliquid fugiat reprehenderit voluptatum?
|
||||||
</p>
|
</p>
|
||||||
|
<p class="center">
|
||||||
|
<a href="/logout">Logout</a>
|
||||||
|
</p>
|
||||||
<h2>Recupero Credenziali Poisson</h2>
|
<h2>Recupero Credenziali Poisson</h2>
|
||||||
<p>
|
<p>
|
||||||
Per il recupero credenziali vieni direttamente al PHC a parlarne con calma con noi altrimenti puoi inviaci una email all'indirizzo <a href="mailto:{{ .Config.Email }}">{{ .Config.Email }}</a> e poi recuperare le nuove credenziali sul sito <a href="https://credenziali.phc.dm.unipi.it/">credenziali.phc.dm.unipi.it</a>.
|
Per il recupero credenziali vieni direttamente al PHC a parlarne con calma con noi altrimenti puoi inviaci una email all'indirizzo <a href="mailto:{{ .Config.Email }}">{{ .Config.Email }}</a> e poi recuperare le nuove credenziali sul sito <a href="https://credenziali.phc.dm.unipi.it/">credenziali.phc.dm.unipi.it</a>.
|
||||||
|
|||||||
Reference in New Issue
Block a user