mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-03 09:51:39 +00:00
(no commit message provided)
This commit is contained in:
committed by
Prad Nukala (aider)
parent
5fd43dfd6b
commit
2f976209db
@@ -1,38 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
var ccref *cacheStore
|
||||
|
||||
func CacheStores(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if ccref == nil {
|
||||
initCacheStore()
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
type cacheStore struct {
|
||||
Challenges *cache.Cache
|
||||
Paths *cache.Cache
|
||||
CIDs *cache.Cache
|
||||
}
|
||||
|
||||
func initCacheStore() {
|
||||
ccref = &cacheStore{
|
||||
Challenges: cache.New(5*time.Minute, 10*time.Minute),
|
||||
Paths: cache.New(30*time.Minute, 1*time.Hour),
|
||||
CIDs: cache.New(30*time.Minute, 1*time.Hour),
|
||||
}
|
||||
}
|
||||
|
||||
func cacheKey(e echo.Context, key string) string {
|
||||
return fmt.Sprintf(SessionID(e), ".", key)
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/segmentio/ksuid"
|
||||
)
|
||||
|
||||
func SessionCookies(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if val := readCookie(c, "session"); val == "" {
|
||||
writeCookie(c, "session", ksuid.New().String())
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
func readCookie(c echo.Context, key string) string {
|
||||
cookie, err := c.Cookie(key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if cookie == nil {
|
||||
return ""
|
||||
}
|
||||
return cookie.Value
|
||||
}
|
||||
|
||||
func writeCookie(c echo.Context, key string, value string) {
|
||||
cookie := new(http.Cookie)
|
||||
cookie.Name = key
|
||||
cookie.Value = value
|
||||
cookie.Expires = time.Now().Add(24 * time.Hour)
|
||||
c.SetCookie(cookie)
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gopkg.in/macaroon.v2"
|
||||
)
|
||||
|
||||
// TODO: Replace session authentication with macroons.
|
||||
func GenerateSessionMacaroon(userId string, audience string) (*macaroon.Macaroon, error) {
|
||||
m, err := macaroon.New([]byte("secret-key"), []byte(userId), "sonr-oidc", macaroon.LatestVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = m.AddFirstPartyCaveat([]byte("exp=" + time.Now().Add(1*time.Hour).Format(time.RFC3339)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = m.AddFirstPartyCaveat([]byte("aud=" + audience))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func ValidateSessionMacaroon(m *macaroon.Macaroon) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Render renders a templ.Component
|
||||
func Render(c echo.Context, cmp templ.Component) error {
|
||||
c.Response().Writer.WriteHeader(http.StatusOK)
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
|
||||
return cmp.Render(c.Request().Context(), c.Response())
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/go-webauthn/webauthn/protocol"
|
||||
"github.com/ipfs/boxo/path"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
func SessionID(c echo.Context) string {
|
||||
return readCookie(c, "session")
|
||||
}
|
||||
|
||||
var Cache = cacheHandler{}
|
||||
|
||||
type cacheHandler struct{}
|
||||
|
||||
func (c cacheHandler) GetChallenge(e echo.Context) protocol.URLEncodedBase64 {
|
||||
key := cacheKey(e, "challenge")
|
||||
if x, found := ccref.Challenges.Get(key); found {
|
||||
chalbz := x.([]byte)
|
||||
chal := protocol.URLEncodedBase64{}
|
||||
err := chal.UnmarshalJSON(chalbz)
|
||||
if err != nil {
|
||||
return chal
|
||||
}
|
||||
return chal
|
||||
}
|
||||
chal, _ := protocol.CreateChallenge()
|
||||
// Save challenge
|
||||
str, err := chal.MarshalJSON()
|
||||
ccref.Challenges.Set(key, str, cache.DefaultExpiration)
|
||||
if err != nil {
|
||||
return chal
|
||||
}
|
||||
return chal
|
||||
}
|
||||
|
||||
func (c cacheHandler) GetLocalPath(e echo.Context) string {
|
||||
key := cacheKey(e, "localPath")
|
||||
if x, found := ccref.Paths.Get(key); found {
|
||||
return x.(string)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c cacheHandler) GetRemoteCID(e echo.Context) path.Path {
|
||||
key := cacheKey(e, "remoteCID")
|
||||
if x, found := ccref.CIDs.Get(key); found {
|
||||
return x.(path.Path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c cacheHandler) SetLocalPath(e echo.Context, path string) {
|
||||
key := cacheKey(e, "localPath")
|
||||
ccref.Paths.Set(key, path, cache.DefaultExpiration)
|
||||
}
|
||||
|
||||
func (c cacheHandler) SetRemoteCID(e echo.Context, path path.Path) {
|
||||
key := cacheKey(e, "remoteCID")
|
||||
ccref.CIDs.Set(key, path, cache.DefaultExpiration)
|
||||
}
|
||||
Reference in New Issue
Block a user