forked from TrueCloudLab/certificates
Add authority.MustFromContext
This commit is contained in:
parent
d5070ecf31
commit
48e2fabeb8
2 changed files with 9 additions and 14 deletions
|
@ -52,15 +52,9 @@ type Authority interface {
|
|||
Version() authority.Version
|
||||
}
|
||||
|
||||
var errAuthority = errors.New("authority is not in context")
|
||||
|
||||
// mustAuthority will be replaced on unit tests.
|
||||
var mustAuthority = func(ctx context.Context) Authority {
|
||||
a, ok := authority.FromContext(ctx)
|
||||
if !ok {
|
||||
panic(errAuthority)
|
||||
}
|
||||
return a
|
||||
return authority.MustFromContext(ctx)
|
||||
}
|
||||
|
||||
// TimeDuration is an alias of provisioner.TimeDuration
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"crypto/x509"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -167,12 +166,14 @@ func FromContext(ctx context.Context) (a *Authority, ok bool) {
|
|||
return
|
||||
}
|
||||
|
||||
// Middleware adds the current authority to the request context.
|
||||
func (a *Authority) Middleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := NewContext(r.Context(), a)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
// MustFromContext returns the current authority from the given context. It will
|
||||
// panic if the authority is not in the context.
|
||||
func MustFromContext(ctx context.Context) *Authority {
|
||||
if a, ok := FromContext(ctx); !ok {
|
||||
panic("authority is not in the context")
|
||||
} else {
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
// reloadAdminResources reloads admins and provisioners from the DB.
|
||||
|
|
Loading…
Reference in a new issue