GoBlog/indieAuthServer.go

240 lines
7.8 KiB
Go
Raw Normal View History

2020-10-13 19:35:39 +00:00
package main
import (
"database/sql"
2021-01-21 16:59:47 +00:00
"encoding/json"
2020-10-13 19:35:39 +00:00
"errors"
"net/http"
"net/url"
"strings"
"time"
2021-11-23 14:23:01 +00:00
"github.com/google/uuid"
"github.com/hacdias/indieauth"
"go.goblog.app/app/pkgs/contenttype"
2020-10-13 19:35:39 +00:00
)
// https://www.w3.org/TR/indieauth/
2020-12-09 16:25:09 +00:00
// https://indieauth.spec.indieweb.org/
2020-10-13 19:35:39 +00:00
2021-11-23 14:23:01 +00:00
var (
errInvalidToken = errors.New("invalid token or token not found")
errInvalidCode = errors.New("invalid code or code not found")
)
2020-10-13 19:35:39 +00:00
2021-11-23 14:23:01 +00:00
// Parse Authorization Request
// https://indieauth.spec.indieweb.org/#authorization-request
func (a *goBlog) indieAuthRequest(w http.ResponseWriter, r *http.Request) {
2021-11-23 14:23:01 +00:00
iareq, err := a.ias.ParseAuthorization(r)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2021-02-08 17:51:07 +00:00
return
}
2021-11-23 14:23:01 +00:00
// Render page that let's the user authorize the app
a.render(w, r, a.renderIndieAuth, &renderData{
2021-11-23 14:23:01 +00:00
Data: iareq,
})
}
2021-11-23 14:23:01 +00:00
// The user accepted the authorization request
// Authorization response
// https://indieauth.spec.indieweb.org/#authorization-response
func (a *goBlog) indieAuthAccept(w http.ResponseWriter, r *http.Request) {
2021-11-23 14:23:01 +00:00
iareq, err := a.ias.ParseAuthorization(r)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2021-02-08 17:51:07 +00:00
return
}
2021-11-23 14:23:01 +00:00
// Save the authorization request
code, err := a.db.indieAuthSaveAuthRequest(iareq)
2020-10-13 19:35:39 +00:00
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2020-10-13 19:35:39 +00:00
return
}
2021-11-23 14:23:01 +00:00
// Build a redirect
query := url.Values{}
query.Set("code", code)
query.Set("state", iareq.State)
http.Redirect(w, r, iareq.RedirectURI+"?"+query.Encode(), http.StatusFound)
2020-10-13 19:35:39 +00:00
}
type tokenResponse struct {
2021-11-23 14:23:01 +00:00
Me string `json:"me,omitempty"`
ClientID string `json:"client_id,omitempty"`
Scope string `json:"scope,omitempty"`
Token string `json:"access_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
// authorization endpoint
// https://indieauth.spec.indieweb.org/#redeeming-the-authorization-code
// The client only exchanges the authorization code for the user's profile URL
func (a *goBlog) indieAuthVerificationAuth(w http.ResponseWriter, r *http.Request) {
2021-02-08 17:51:07 +00:00
if err := r.ParseForm(); err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2021-02-08 17:51:07 +00:00
return
}
2021-11-23 14:23:01 +00:00
a.indieAuthVerification(w, r, false)
}
// token endpoint
// https://indieauth.spec.indieweb.org/#redeeming-the-authorization-code
// The client exchanges the authorization code for an access token and the user's profile URL
func (a *goBlog) indieAuthVerificationToken(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
2020-12-09 16:25:09 +00:00
}
2021-11-23 14:23:01 +00:00
// Token Revocation
if r.Form.Get("action") == "revoke" {
a.db.indieAuthRevokeToken(r.Form.Get("token"))
w.WriteHeader(http.StatusOK)
return
}
// Token request
a.indieAuthVerification(w, r, true)
}
// Verify the authorization request with or without token response
func (a *goBlog) indieAuthVerification(w http.ResponseWriter, r *http.Request, withToken bool) {
// Get code and retrieve auth request
code := r.Form.Get("code")
if code == "" {
a.serveError(w, r, "missing code parameter", http.StatusBadRequest)
return
}
data, err := a.db.indieAuthGetAuthRequest(code)
if errors.Is(err, errInvalidCode) {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
} else if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2020-12-09 16:25:09 +00:00
return
}
2021-11-23 14:23:01 +00:00
// Check grant type
if grantType := r.Form.Get("grant_type"); grantType != "" && grantType != "authorization_code" {
a.serveError(w, r, "unknown grant type", http.StatusBadRequest)
return
}
// Validate token exchange
if err = a.ias.ValidateTokenExchange(data, r); err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
2020-12-09 16:25:09 +00:00
return
}
2021-11-23 14:23:01 +00:00
// Generate response
resp := &tokenResponse{
2021-11-22 22:21:47 +00:00
Me: a.getFullAddress("") + "/", // MUST contain a path component / trailing slash
2021-11-23 14:23:01 +00:00
}
if withToken {
// Generate and save token
token, err := a.db.indieAuthSaveToken(data)
2020-10-13 19:35:39 +00:00
if err != nil {
2021-11-23 14:23:01 +00:00
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2020-11-11 08:03:20 +00:00
return
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
// Add token to response
resp.TokenType = "Bearer"
resp.Token = token
resp.Scope = strings.Join(data.Scopes, " ")
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
b, _ := json.Marshal(resp)
w.Header().Set(contentType, contenttype.JSONUTF8)
_, _ = a.min.Write(w, contenttype.JSON, b)
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
// Save the authorization request and return the code
func (db *database) indieAuthSaveAuthRequest(data *indieauth.AuthenticationRequest) (string, error) {
// Generate a code to identify the request
code := uuid.NewString()
// Save the request
_, err := db.exec(
"insert into indieauthauth (time, code, client, redirect, scope, challenge, challengemethod) values (?, ?, ?, ?, ?, ?, ?)",
time.Now().UTC().Unix(), code, data.ClientID, data.RedirectURI, strings.Join(data.Scopes, " "), data.CodeChallenge, data.CodeChallengeMethod,
)
return code, err
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
// Retrieve the auth request from the database to continue the authorization process
func (db *database) indieAuthGetAuthRequest(code string) (data *indieauth.AuthenticationRequest, err error) {
// code valid for 10 minutes
maxAge := time.Now().UTC().Add(-10 * time.Minute).Unix()
// Query the database
row, err := db.queryRow("select client, redirect, scope, challenge, challengemethod from indieauthauth where time >= ? and code = ?", maxAge, code)
2020-12-09 16:25:09 +00:00
if err != nil {
2021-11-23 14:23:01 +00:00
return nil, err
2020-12-09 16:25:09 +00:00
}
2021-11-23 14:23:01 +00:00
data = &indieauth.AuthenticationRequest{}
var scope string
err = row.Scan(&data.ClientID, &data.RedirectURI, &scope, &data.CodeChallenge, &data.CodeChallengeMethod)
2020-12-09 16:25:09 +00:00
if err == sql.ErrNoRows {
2021-11-23 14:23:01 +00:00
return nil, errInvalidCode
2020-12-09 16:25:09 +00:00
} else if err != nil {
2021-11-23 14:23:01 +00:00
return nil, err
2020-12-09 16:25:09 +00:00
}
if scope != "" {
data.Scopes = strings.Split(scope, " ")
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
// Delete the auth code and expired auth codes
_, _ = db.exec("delete from indieauthauth where code = ? or time < ?", code, maxAge)
return data, nil
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
// Access token verification request (https://indieauth.spec.indieweb.org/#access-token-verification-request)
//
// GET request to the token endpoint to check if the access token is valid
func (a *goBlog) indieAuthTokenVerification(w http.ResponseWriter, r *http.Request) {
data, err := a.db.indieAuthVerifyToken(r.Header.Get("Authorization"))
if errors.Is(err, errInvalidToken) {
a.serveError(w, r, err.Error(), http.StatusUnauthorized)
return
} else if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
res := &tokenResponse{
Scope: strings.Join(data.Scopes, " "),
Me: a.getFullAddress("") + "/", // MUST contain a path component / trailing slash
ClientID: data.ClientID,
}
w.Header().Set(contentType, contenttype.JSONUTF8)
b, _ := json.Marshal(res)
_, _ = a.min.Write(w, contenttype.JSON, b)
2020-10-13 19:35:39 +00:00
}
2021-11-23 14:23:01 +00:00
// Checks the database for the token and returns the indieAuthData with client and scope.
//
// Returns errInvalidToken if the token is invalid.
func (db *database) indieAuthVerifyToken(token string) (data *indieauth.AuthenticationRequest, err error) {
2020-10-13 19:35:39 +00:00
token = strings.ReplaceAll(token, "Bearer ", "")
2021-11-23 14:23:01 +00:00
data = &indieauth.AuthenticationRequest{Scopes: []string{}}
row, err := db.queryRow("select client, scope from indieauthtoken where token = @token", sql.Named("token", token))
if err != nil {
return nil, err
}
2021-11-23 14:23:01 +00:00
var scope string
err = row.Scan(&data.ClientID, &scope)
2020-10-13 19:35:39 +00:00
if err == sql.ErrNoRows {
2021-11-23 14:23:01 +00:00
return nil, errInvalidToken
2020-10-13 19:35:39 +00:00
} else if err != nil {
return nil, err
}
if scope != "" {
data.Scopes = strings.Split(scope, " ")
}
return
}
2021-11-23 14:23:01 +00:00
// Save a new token to the database
func (db *database) indieAuthSaveToken(data *indieauth.AuthenticationRequest) (string, error) {
token := uuid.NewString()
_, err := db.exec("insert into indieauthtoken (time, token, client, scope) values (?, ?, ?, ?)", time.Now().UTC().Unix(), token, data.ClientID, strings.Join(data.Scopes, " "))
return token, err
}
// Revoke and delete the token from the database
func (db *database) indieAuthRevokeToken(token string) {
2021-02-08 17:51:07 +00:00
if token != "" {
_, _ = db.exec("delete from indieauthtoken where token=?", token)
2020-10-13 19:35:39 +00:00
}
}