Skip to content
Snippets Groups Projects
Select Git revision
  • 23e363c2c9f0e5d6aee2d18c59c026a8f9217adb
  • ballinvoher default protected
  • client-http-server-for-token
  • master
  • gitlab-auth-issue
  • windows
  • microsoft
  • message
  • azure_auth
  • prometheus
  • permission-templates
  • no-datastore
  • save-public-keys
  • gitlab-group-level-start
  • v1.1.0
  • v1.0.0
  • v0.1
17 results

handlers.go

Blame
  • handlers.go 4.93 KiB
    package server
    
    import (
    	"crypto/rand"
    	"encoding/hex"
    	"encoding/json"
    	"fmt"
    	"html/template"
    	"io"
    	"log"
    	"net/http"
    	"strconv"
    	"strings"
    
    	"github.com/gorilla/csrf"
    	"github.com/nsheridan/cashier/lib"
    	"github.com/nsheridan/cashier/server/store"
    	"github.com/nsheridan/cashier/server/templates"
    	"github.com/pkg/errors"
    	"golang.org/x/oauth2"
    )
    
    func (a *app) sign(w http.ResponseWriter, r *http.Request) {
    	var t string
    	if ah := r.Header.Get("Authorization"); ah != "" {
    		if len(ah) > 6 && strings.ToUpper(ah[0:7]) == "BEARER " {
    			t = ah[7:]
    		}
    	}
    
    	token := &oauth2.Token{
    		AccessToken: t,
    	}
    	if !a.authprovider.Valid(token) {
    		w.WriteHeader(http.StatusUnauthorized)
    		fmt.Fprint(w, http.StatusText(http.StatusUnauthorized))
    		return
    	}
    
    	// Sign the pubkey and issue the cert.
    	req := &lib.SignRequest{}
    	if err := json.NewDecoder(r.Body).Decode(req); err != nil {
    		fmt.Println(err)
    		w.WriteHeader(http.StatusBadRequest)
    		fmt.Fprint(w, http.StatusText(http.StatusBadRequest))
    		return
    	}
    
    	if a.requireReason && req.Message == "" {
    		w.Header().Add("X-Need-Reason", "required")
    		w.WriteHeader(http.StatusForbidden)
    		fmt.Fprint(w, http.StatusText(http.StatusForbidden))
    		return
    	}
    
    	username := a.authprovider.Username(token)
    	a.authprovider.Revoke(token) // We don't need this anymore.
    	cert, err := a.keysigner.SignUserKey(req, username)
    	if err != nil {
    		w.WriteHeader(http.StatusInternalServerError)
    		fmt.Fprintf(w, "Error signing key")
    		return
    	}
    
    	rec := store.MakeRecord(cert)
    	rec.Message = req.Message
    	if err := a.certstore.SetRecord(rec); err != nil {
    		log.Printf("Error recording cert: %v", err)
    	}
    	if err := json.NewEncoder(w).Encode(&lib.SignResponse{