Skip to content

Commit

Permalink
fix: JWT Signing key will use hashed Discord App Client ID to prevent…
Browse files Browse the repository at this point in the history
… breaking on server reboot
  • Loading branch information
VeryStrongFingers committed Jan 25, 2024
1 parent 43fbe4e commit 1b3219f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
24 changes: 24 additions & 0 deletions hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package caddydiscord

import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
)

func hashString512(input string) string {
hasher := sha512.New()
hasher.Write([]byte(input))
return hex.EncodeToString(hasher.Sum(nil))
}

func hashString256(input string, length int) string {
hasher := sha256.New()
hasher.Write([]byte(input))
fullHash := hex.EncodeToString(hasher.Sum(nil))

if length > len(fullHash) {
length = len(fullHash)
}
return fullHash[:length]
}
9 changes: 7 additions & 2 deletions module_app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package caddydiscord

import (
"encoding/hex"
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
Expand Down Expand Up @@ -31,6 +30,7 @@ type DiscordPortalApp struct {
Realms RealmRegistry `json:"realms"`
oauthConfig *oauth2.Config
Key string `json:"key,omitempty"`
Signature string `json:"signature,omitempty"`
}

// CaddyModule returns the Caddy module information.
Expand All @@ -42,7 +42,12 @@ func (DiscordPortalApp) CaddyModule() caddy.ModuleInfo {
}

func (d *DiscordPortalApp) Provision(_ caddy.Context) error {
d.Key = hex.EncodeToString(randomness(64))
// Discord App ID is used as entropy for JWT signing keys.
d.Key = hashString512(d.ClientID)

// TODO: Signature will be used for cookie integrity checks, to ensure checks are inline with most recent Caddyfile.
// TODOTODO: Use parsed caddyfile signature for checks, instead of just Discord App Client ID.
d.Signature = hashString256(d.ClientID, 16)
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions module_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type DiscordAuthPlugin struct {
Key string
tokenSigner TokenSignerSignature
flowTokenParser FlowTokenParserSignature
signature string
}

func (DiscordAuthPlugin) CaddyModule() caddy.ModuleInfo {
Expand All @@ -62,6 +63,7 @@ func (s *DiscordAuthPlugin) Provision(ctx caddy.Context) error {

s.tokenSigner = NewTokenSigner(key)
s.flowTokenParser = NewFlowTokenParser(key)
s.signature = app.Signature

return nil
}
Expand Down
2 changes: 2 additions & 0 deletions module_entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (e ProtectorPlugin) Authenticate(w http.ResponseWriter, r *http.Request) (c
q.Del("DISCO_REALM")
r.URL.RawQuery = q.Encode()

// TODO: Expires should be reduced if authorisation failed.

cookie := &http.Cookie{
Name: fmt.Sprintf("%s_%s", cookieName, realm),
Value: signedToken,
Expand Down

0 comments on commit 1b3219f

Please sign in to comment.