Simple blogging system written in Go https://goblog.app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GoBlog/indieAuthServer.go

282 lines
7.9 KiB

2 years ago
package main
import (
"crypto/sha1"
"database/sql"
"encoding/json"
2 years ago
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"git.jlel.se/jlelse/GoBlog/pkgs/contenttype"
2 years ago
"github.com/spf13/cast"
)
// https://www.w3.org/TR/indieauth/
// https://indieauth.spec.indieweb.org/
2 years ago
type indieAuthData struct {
ClientID string
RedirectURI string
State string
Scopes []string
code string
token string
time time.Time
2 years ago
}
func (a *goBlog) indieAuthRequest(w http.ResponseWriter, r *http.Request) {
// Authorization request
2 years ago
if err := r.ParseForm(); err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2 years ago
return
}
data := &indieAuthData{
ClientID: r.Form.Get("client_id"),
RedirectURI: r.Form.Get("redirect_uri"),
State: r.Form.Get("state"),
}
if rt := r.Form.Get("response_type"); rt != "code" && rt != "id" && rt != "" {
a.serveError(w, r, "response_type must be code", http.StatusBadRequest)
return
}
if scope := r.Form.Get("scope"); scope != "" {
data.Scopes = strings.Split(scope, " ")
}
if !isValidProfileURL(data.ClientID) || !isValidProfileURL(data.RedirectURI) {
a.serveError(w, r, "client_id and redirect_uri need to by valid URLs", http.StatusBadRequest)
return
}
if data.State == "" {
a.serveError(w, r, "state must not be empty", http.StatusBadRequest)
return
}
a.render(w, r, "indieauth", &renderData{
Data: data,
})
}
2 years ago
func isValidProfileURL(profileURL string) bool {
u, err := url.Parse(profileURL)
if err != nil {
return false
}
if u.Scheme != "http" && u.Scheme != "https" {
return false
}
if u.Fragment != "" {
return false
}
if u.User.String() != "" {
return false
}
if u.Port() != "" {
return false
}
// Missing: Check domain / ip
return true
}
func (a *goBlog) indieAuthAccept(w http.ResponseWriter, r *http.Request) {
2 years ago
// Authentication flow
2 years ago
if err := r.ParseForm(); err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2 years ago
return
}
2 years ago
data := &indieAuthData{
ClientID: r.Form.Get("client_id"),
RedirectURI: r.Form.Get("redirect_uri"),
State: r.Form.Get("state"),
Scopes: r.Form["scopes"],
time: time.Now(),
2 years ago
}
sha := sha1.New()
2 years ago
if _, err := sha.Write([]byte(data.time.String() + data.ClientID)); err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2 years ago
return
}
2 years ago
data.code = fmt.Sprintf("%x", sha.Sum(nil))
err := a.db.saveAuthorization(data)
2 years ago
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2 years ago
return
}
http.Redirect(w, r, data.RedirectURI+"?code="+data.code+"&state="+data.State, http.StatusFound)
}
type tokenResponse struct {
AccessToken string `json:"access_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
Scope string `json:"scope,omitempty"`
Me string `json:"me,omitempty"`
ClientID string `json:"client_id,omitempty"`
}
func (a *goBlog) indieAuthVerification(w http.ResponseWriter, r *http.Request) {
// Authorization verification
2 years ago
if err := r.ParseForm(); err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2 years ago
return
}
data := &indieAuthData{
code: r.Form.Get("code"),
ClientID: r.Form.Get("client_id"),
RedirectURI: r.Form.Get("redirect_uri"),
}
valid, err := a.db.verifyAuthorization(data)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
if !valid {
a.serveError(w, r, "Authentication not valid", http.StatusForbidden)
return
}
b, _ := json.Marshal(tokenResponse{
Me: a.cfg.Server.PublicAddress,
})
w.Header().Set(contentType, contenttype.JSONUTF8)
_, _ = a.min.Write(w, contenttype.JSON, b)
}
func (a *goBlog) indieAuthToken(w http.ResponseWriter, r *http.Request) {
2 years ago
if r.Method == http.MethodGet {
// Token verification
data, err := a.db.verifyIndieAuthToken(r.Header.Get("Authorization"))
2 years ago
if err != nil {
a.serveError(w, r, "Invalid token or token not found", http.StatusUnauthorized)
2 years ago
return
2 years ago
}
res := &tokenResponse{
Scope: strings.Join(data.Scopes, " "),
Me: a.cfg.Server.PublicAddress,
2 years ago
ClientID: data.ClientID,
}
b, _ := json.Marshal(res)
w.Header().Set(contentType, contenttype.JSONUTF8)
_, _ = a.min.Write(w, contenttype.JSON, b)
2 years ago
return
2 years ago
} else if r.Method == http.MethodPost {
2 years ago
if err := r.ParseForm(); err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2 years ago
return
}
2 years ago
// Token Revocation
if r.Form.Get("action") == "revoke" {
a.db.revokeIndieAuthToken(r.Form.Get("token"))
2 years ago
w.WriteHeader(http.StatusOK)
return
}
// Token request
if r.Form.Get("grant_type") == "authorization_code" {
data := &indieAuthData{
code: r.Form.Get("code"),
ClientID: r.Form.Get("client_id"),
RedirectURI: r.Form.Get("redirect_uri"),
}
valid, err := a.db.verifyAuthorization(data)
2 years ago
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2 years ago
return
}
if !valid {
a.serveError(w, r, "Authentication not valid", http.StatusForbidden)
2 years ago
return
}
if len(data.Scopes) < 1 {
a.serveError(w, r, "No scope", http.StatusBadRequest)
2 years ago
return
}
data.time = time.Now()
sha := sha1.New()
2 years ago
if _, err := sha.Write([]byte(data.time.String() + data.ClientID)); err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2 years ago
return
}
2 years ago
data.token = fmt.Sprintf("%x", sha.Sum(nil))
err = a.db.saveToken(data)
2 years ago
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2 years ago
return
}
res := &tokenResponse{
TokenType: "Bearer",
AccessToken: data.token,
Scope: strings.Join(data.Scopes, " "),
Me: a.cfg.Server.PublicAddress,
2 years ago
}
b, _ := json.Marshal(res)
w.Header().Set(contentType, contenttype.JSONUTF8)
_, _ = a.min.Write(w, contenttype.JSON, b)
2 years ago
return
2 years ago
}
a.serveError(w, r, "", http.StatusBadRequest)
2 years ago
return
2 years ago
}
}
func (db *database) saveAuthorization(data *indieAuthData) (err error) {
_, err = db.exec("insert into indieauthauth (time, code, client, redirect, scope) values (?, ?, ?, ?, ?)", data.time.Unix(), data.code, data.ClientID, data.RedirectURI, strings.Join(data.Scopes, " "))
2 years ago
return
}
func (db *database) verifyAuthorization(data *indieAuthData) (valid bool, err error) {
2 years ago
// code valid for 600 seconds
row, err := db.queryRow("select code, client, redirect, scope from indieauthauth where time >= ? and code = ? and client = ? and redirect = ?", time.Now().Unix()-600, data.code, data.ClientID, data.RedirectURI)
if err != nil {
return false, err
}
scope := ""
err = row.Scan(&data.code, &data.ClientID, &data.RedirectURI, &scope)
if err == sql.ErrNoRows {
return false, nil
} else if err != nil {
return false, err
}
if scope != "" {
data.Scopes = strings.Split(scope, " ")
2 years ago
}
valid = true
_, err = db.exec("delete from indieauthauth where code = ? or time < ?", data.code, time.Now().Unix()-600)
2 years ago
data.code = ""
return
}
func (db *database) saveToken(data *indieAuthData) (err error) {
_, err = db.exec("insert into indieauthtoken (time, token, client, scope) values (?, ?, ?, ?)", data.time.Unix(), data.token, data.ClientID, strings.Join(data.Scopes, " "))
2 years ago
return
}
func (db *database) verifyIndieAuthToken(token string) (data *indieAuthData, err error) {
2 years ago
token = strings.ReplaceAll(token, "Bearer ", "")
2 years ago
data = &indieAuthData{
Scopes: []string{},
}
row, err := db.queryRow("select time, token, client, scope from indieauthtoken where token = @token", sql.Named("token", token))
if err != nil {
return nil, err
}
2 years ago
timeString := ""
scope := ""
err = row.Scan(&timeString, &data.token, &data.ClientID, &scope)
2 years ago
if err == sql.ErrNoRows {
return nil, errors.New("token not found")
} else if err != nil {
return nil, err
}
if scope != "" {
data.Scopes = strings.Split(scope, " ")
}
data.time = time.Unix(cast.ToInt64(timeString), 0)
return
}
func (db *database) revokeIndieAuthToken(token string) {
2 years ago
if token != "" {
_, _ = db.exec("delete from indieauthtoken where token=?", token)
2 years ago
}
}