Update examples/auth.go
This commit is contained in:
parent
e4dea6f2c8
commit
8e442146c3
|
@ -51,19 +51,21 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSession(crt *x509.Certificate) (*session, bool) {
|
func getSession(cert *x509.Certificate) (*session, bool) {
|
||||||
fingerprint := gemini.Fingerprint(crt)
|
fingerprint := gemini.Fingerprint(cert)
|
||||||
session, ok := sessions[fingerprint]
|
session, ok := sessions[fingerprint]
|
||||||
return session, ok
|
return session, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func login(w *gemini.ResponseWriter, r *gemini.Request) {
|
func login(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
cert, ok := gemini.Certificate(w, r)
|
cert := gemini.Certificate(r)
|
||||||
if !ok {
|
if cert == nil {
|
||||||
|
w.WriteStatus(gemini.StatusCertificateRequired)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
username, ok := gemini.Input(w, r, "Username")
|
username, ok := gemini.Input(r)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
w.WriteHeader(gemini.StatusInput, "Username")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fingerprint := gemini.Fingerprint(cert)
|
fingerprint := gemini.Fingerprint(cert)
|
||||||
|
@ -74,8 +76,9 @@ func login(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginPassword(w *gemini.ResponseWriter, r *gemini.Request) {
|
func loginPassword(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
cert, ok := gemini.Certificate(w, r)
|
cert := gemini.Certificate(r)
|
||||||
if !ok {
|
if cert == nil {
|
||||||
|
w.WriteStatus(gemini.StatusCertificateRequired)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
session, ok := getSession(cert)
|
session, ok := getSession(cert)
|
||||||
|
@ -84,8 +87,9 @@ func loginPassword(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
password, ok := gemini.SensitiveInput(w, r, "Password")
|
password, ok := gemini.Input(r)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
w.WriteHeader(gemini.StatusSensitiveInput, "Password")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
expected := logins[session.username].password
|
expected := logins[session.username].password
|
||||||
|
@ -93,13 +97,14 @@ func loginPassword(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
session.authorized = true
|
session.authorized = true
|
||||||
w.WriteHeader(gemini.StatusRedirect, "/profile")
|
w.WriteHeader(gemini.StatusRedirect, "/profile")
|
||||||
} else {
|
} else {
|
||||||
gemini.SensitiveInput(w, r, "Wrong password. Try again")
|
w.WriteHeader(gemini.StatusSensitiveInput, "Wrong password. Try again")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func logout(w *gemini.ResponseWriter, r *gemini.Request) {
|
func logout(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
cert, ok := gemini.Certificate(w, r)
|
cert := gemini.Certificate(r)
|
||||||
if !ok {
|
if cert == nil {
|
||||||
|
w.WriteStatus(gemini.StatusCertificateRequired)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fingerprint := gemini.Fingerprint(cert)
|
fingerprint := gemini.Fingerprint(cert)
|
||||||
|
@ -108,8 +113,9 @@ func logout(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func profile(w *gemini.ResponseWriter, r *gemini.Request) {
|
func profile(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
cert, ok := gemini.Certificate(w, r)
|
cert := gemini.Certificate(r)
|
||||||
if !ok {
|
if cert == nil {
|
||||||
|
w.WriteStatus(gemini.StatusCertificateRequired)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
session, ok := getSession(cert)
|
session, ok := getSession(cert)
|
||||||
|
@ -124,8 +130,9 @@ func profile(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func admin(w *gemini.ResponseWriter, r *gemini.Request) {
|
func admin(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
cert, ok := gemini.Certificate(w, r)
|
cert := gemini.Certificate(r)
|
||||||
if !ok {
|
if cert == nil {
|
||||||
|
w.WriteStatus(gemini.StatusCertificateRequired)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
session, ok := getSession(cert)
|
session, ok := getSession(cert)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user