forked from tangled.org/core
this repo has no description

rename consts

Changed files
+33 -35
appview
+14 -14
appview/auth/auth.go
···
}
func Make() (*Auth, error) {
-
store := sessions.NewCookieStore([]byte(appview.SESSION_COOKIE_SECRET))
+
store := sessions.NewCookieStore([]byte(appview.SessionCookieSecret))
return &Auth{store}, nil
}
···
}
func (a *Auth) StoreSession(r *http.Request, w http.ResponseWriter, atSessionish Sessionish, pdsEndpoint string) error {
-
clientSession, _ := a.Store.Get(r, appview.SESSION_NAME)
-
clientSession.Values[appview.SESSION_HANDLE] = atSessionish.GetHandle()
-
clientSession.Values[appview.SESSION_DID] = atSessionish.GetDid()
-
clientSession.Values[appview.SESSION_PDS] = pdsEndpoint
-
clientSession.Values[appview.SESSION_ACCESSJWT] = atSessionish.GetAccessJwt()
-
clientSession.Values[appview.SESSION_REFRESHJWT] = atSessionish.GetRefreshJwt()
-
clientSession.Values[appview.SESSION_EXPIRY] = time.Now().Add(time.Hour).Format(appview.TIME_LAYOUT)
-
clientSession.Values[appview.SESSION_AUTHENTICATED] = true
+
clientSession, _ := a.Store.Get(r, appview.SessionName)
+
clientSession.Values[appview.SessionHandle] = atSessionish.GetHandle()
+
clientSession.Values[appview.SessionDid] = atSessionish.GetDid()
+
clientSession.Values[appview.SessionPds] = pdsEndpoint
+
clientSession.Values[appview.SessionAccessJwt] = atSessionish.GetAccessJwt()
+
clientSession.Values[appview.SessionRefreshJwt] = atSessionish.GetRefreshJwt()
+
clientSession.Values[appview.SessionExpiry] = time.Now().Add(time.Hour).Format(appview.TimeLayout)
+
clientSession.Values[appview.SessionAuthenticated] = true
return clientSession.Save(r, w)
}
···
}
func (a *Auth) GetSession(r *http.Request) (*sessions.Session, error) {
-
return a.Store.Get(r, appview.SESSION_NAME)
+
return a.Store.Get(r, appview.SessionName)
}
func (a *Auth) GetDID(r *http.Request) string {
-
clientSession, _ := a.Store.Get(r, appview.SESSION_NAME)
-
return clientSession.Values[appview.SESSION_DID].(string)
+
clientSession, _ := a.Store.Get(r, appview.SessionName)
+
return clientSession.Values[appview.SessionDid].(string)
}
func (a *Auth) GetHandle(r *http.Request) string {
-
clientSession, _ := a.Store.Get(r, appview.SESSION_NAME)
-
return clientSession.Values[appview.SESSION_HANDLE].(string)
+
clientSession, _ := a.Store.Get(r, appview.SessionName)
+
return clientSession.Values[appview.SessionHandle].(string)
}
+10 -12
appview/consts.go
···
package appview
const (
-
SESSION_COOKIE_SECRET = "TODO_CHANGE_ME"
-
SESSION_NAME = "appview-session"
-
SESSION_HANDLE = "handle"
-
SESSION_DID = "did"
-
SESSION_PDS = "pds"
-
SESSION_ACCESSJWT = "accessJwt"
-
SESSION_REFRESHJWT = "refreshJwt"
-
SESSION_EXPIRY = "expiry"
-
SESSION_AUTHENTICATED = "authenticated"
-
-
SALT = "TODO_RANDOM_SALT"
-
TIME_LAYOUT = "2006-01-02 15:04:05.999999999 -0700 MST"
+
SessionCookieSecret = "TODO_CHANGE_ME"
+
SessionName = "appview-session"
+
SessionHandle = "handle"
+
SessionDid = "did"
+
SessionPds = "pds"
+
SessionAccessJwt = "accessJwt"
+
SessionRefreshJwt = "refreshJwt"
+
SessionExpiry = "expiry"
+
SessionAuthenticated = "authenticated"
+
TimeLayout = "2006-01-02 15:04:05.999999999 -0700 MST"
)
+7 -7
appview/state/middleware.go
···
func AuthMiddleware(s *State) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
session, _ := s.auth.Store.Get(r, appview.SESSION_NAME)
-
authorized, ok := session.Values[appview.SESSION_AUTHENTICATED].(bool)
+
session, _ := s.auth.Store.Get(r, appview.SessionName)
+
authorized, ok := session.Values[appview.SessionAuthenticated].(bool)
if !ok || !authorized {
log.Printf("not logged in, redirecting")
···
// refresh if nearing expiry
// TODO: dedup with /login
-
expiryStr := session.Values[appview.SESSION_EXPIRY].(string)
-
expiry, err := time.Parse(appview.TIME_LAYOUT, expiryStr)
+
expiryStr := session.Values[appview.SessionExpiry].(string)
+
expiry, err := time.Parse(appview.TimeLayout, expiryStr)
if err != nil {
log.Println("invalid expiry time", err)
return
}
-
pdsUrl := session.Values[appview.SESSION_PDS].(string)
-
did := session.Values[appview.SESSION_DID].(string)
-
refreshJwt := session.Values[appview.SESSION_REFRESHJWT].(string)
+
pdsUrl := session.Values[appview.SessionPds].(string)
+
did := session.Values[appview.SessionDid].(string)
+
refreshJwt := session.Values[appview.SessionRefreshJwt].(string)
if time.Now().After(expiry) {
log.Println("token expired, refreshing ...")
+2 -2
appview/state/state.go
···
return
case http.MethodPost:
-
session, err := s.auth.Store.Get(r, appview.SESSION_NAME)
+
session, err := s.auth.Store.Get(r, appview.SessionName)
if err != nil || session.IsNew {
log.Println("unauthorized attempt to generate registration key")
http.Error(w, "Forbidden", http.StatusUnauthorized)
return
}
-
did := session.Values[appview.SESSION_DID].(string)
+
did := session.Values[appview.SessionDid].(string)
// check if domain is valid url, and strip extra bits down to just host
domain := r.FormValue("domain")