-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add websockets to update ui on change
- Loading branch information
1 parent
fd21373
commit 91603a9
Showing
12 changed files
with
274 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
OIDC_ISSUER_URL=https://auth.dev.stenic.io/auth/realms/dev | ||
OIDC_CLIENT_ID=ledger | ||
OIDC_AUDIENCE=account |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
jwtmiddleware "github.com/auth0/go-jwt-middleware/v2" | ||
"github.com/auth0/go-jwt-middleware/v2/validator" | ||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type LedgerValidator interface { | ||
ValidateToken(authString string) error | ||
GetJWTMiddleware() gin.HandlerFunc | ||
} | ||
|
||
type ledgerValidator struct { | ||
IssuerURL string | ||
Audience []string | ||
} | ||
|
||
func New(cfg ApiSecurityOptions) LedgerValidator { | ||
l := ledgerValidator{ | ||
IssuerURL: cfg.IssuerURL, | ||
Audience: cfg.Audience, | ||
} | ||
|
||
return l | ||
} | ||
|
||
func (l ledgerValidator) ValidateToken(authString string) error { | ||
if len(authString) < 8 && strings.ToLower(authString[:6]) != "bearer" { | ||
return fmt.Errorf("not a bearer auth string") | ||
} | ||
|
||
// Validate local token | ||
logrus.Trace("Checking Local") | ||
_, err := ValidateToken(authString[7:]) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
// OIDC validation | ||
jwtOIDCValidator := getOidcValidatorFunc(l.IssuerURL, l.Audience) | ||
if jwtOIDCValidator == nil { | ||
return fmt.Errorf("token is invalid") | ||
} | ||
|
||
logrus.Trace("Checking OIDC") | ||
_, err = jwtOIDCValidator.ValidateToken(context.Background(), authString[7:]) | ||
return err | ||
} | ||
|
||
func (l ledgerValidator) GetJWTMiddleware() gin.HandlerFunc { | ||
var jwtOidcMiddleware *jwtmiddleware.JWTMiddleware | ||
if l.IssuerURL != "" { | ||
jwtOidcMiddleware = getOidcValidator(l.IssuerURL, l.Audience) | ||
} | ||
|
||
return func(c *gin.Context) { | ||
var handler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { | ||
c.Request = r | ||
c.Next() | ||
} | ||
|
||
if jwtOidcMiddleware != nil { | ||
logrus.Debug("Checking oidc token") | ||
jwtOidcMiddleware.CheckJWT(handler).ServeHTTP(c.Writer, c.Request) | ||
} | ||
|
||
// Continue with local auth if OIDC is not detected | ||
if c.Request.Context().Value(jwtmiddleware.ContextKey{}) == nil { | ||
logrus.Debug("Checking local token") | ||
checkLocalJWT(handler).ServeHTTP(c.Writer, c.Request) | ||
} | ||
} | ||
} | ||
|
||
func TokenFromContext(ctx context.Context) *UserInfo { | ||
if os.Getenv("AUTH_INSECURE") == "yes" { | ||
logrus.Error("Skipping auth, hope you are in dev mode.") | ||
return &UserInfo{ | ||
Username: "Insecure user", | ||
} | ||
} | ||
if raw, valid := ctx.Value(localJwtClaims{}).(*Claims); valid { | ||
return &UserInfo{ | ||
Username: raw.Username, | ||
} | ||
} | ||
|
||
if raw, valid := ctx.Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims); valid { | ||
cc, ok := raw.CustomClaims.(*CustomClaims) | ||
if !ok { | ||
return nil | ||
} | ||
return &UserInfo{ | ||
Username: cc.PreferredUsername, | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package server | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gobwas/ws" | ||
"github.com/gobwas/ws/wsutil" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stenic/ledger/internal/auth" | ||
"github.com/stenic/ledger/internal/pkg/versions" | ||
) | ||
|
||
func wsHandler(authValidator auth.LedgerValidator) gin.HandlerFunc { | ||
logger := logrus.WithFields(logrus.Fields{ | ||
"scope": "websockets", | ||
}) | ||
|
||
var count = *versions.CountTotal() | ||
|
||
ticker := time.NewTicker(5 * time.Second) | ||
go func() { | ||
for { | ||
select { | ||
case <-ticker.C: | ||
count = *versions.CountTotal() | ||
logger.WithField("count", count).Trace("Refreshed version count") | ||
} | ||
} | ||
}() | ||
|
||
return func(c *gin.Context) { | ||
conn, _, _, err := ws.UpgradeHTTP(c.Request, c.Writer) | ||
if err != nil { | ||
logger.Warn(err) | ||
} | ||
logger.Debug("Client connected") | ||
|
||
go func() { | ||
defer conn.Close() | ||
var lastSend = count | ||
|
||
msg, _, err := wsutil.ReadClientData(conn) | ||
if err != nil { | ||
logger.Error(err) | ||
} | ||
if err := authValidator.ValidateToken(string(msg)); err != nil { | ||
logger.Error(err) | ||
return | ||
} | ||
|
||
for { | ||
if lastSend != count { | ||
logger.Debug("Sending refreshVersions") | ||
err = wsutil.WriteServerMessage(conn, ws.OpText, []byte("refreshVersions")) | ||
if err != nil { | ||
if _, ok := err.(wsutil.ClosedError); ok { | ||
logger.Debug("Client disconnected") | ||
} else { | ||
logger.Error(err) | ||
} | ||
return | ||
} | ||
lastSend = count | ||
} | ||
time.Sleep(1 * time.Second) | ||
} | ||
}() | ||
} | ||
} |
Oops, something went wrong.