-11
appview/auth/auth.go
-11
appview/auth/auth.go
···
8
9
comatproto "github.com/bluesky-social/indigo/api/atproto"
10
"github.com/bluesky-social/indigo/atproto/identity"
11
-
"github.com/bluesky-social/indigo/atproto/syntax"
12
"github.com/bluesky-social/indigo/xrpc"
13
"github.com/gorilla/sessions"
14
"github.com/sotangled/tangled/appview"
···
31
func Make() (*Auth, error) {
32
store := sessions.NewCookieStore([]byte(appview.SessionCookieSecret))
33
return &Auth{store}, nil
34
-
}
35
-
36
-
func ResolveIdent(ctx context.Context, arg string) (*identity.Identity, error) {
37
-
id, err := syntax.ParseAtIdentifier(arg)
38
-
if err != nil {
39
-
return nil, err
40
-
}
41
-
42
-
dir := identity.DefaultDirectory()
43
-
return dir.Lookup(ctx, *id)
44
}
45
46
func (a *Auth) CreateInitialSession(ctx context.Context, resolved *identity.Identity, appPassword string) (*comatproto.ServerCreateSession_Output, error) {
···
8
9
comatproto "github.com/bluesky-social/indigo/api/atproto"
10
"github.com/bluesky-social/indigo/atproto/identity"
11
"github.com/bluesky-social/indigo/xrpc"
12
"github.com/gorilla/sessions"
13
"github.com/sotangled/tangled/appview"
···
30
func Make() (*Auth, error) {
31
store := sessions.NewCookieStore([]byte(appview.SessionCookieSecret))
32
return &Auth{store}, nil
33
}
34
35
func (a *Auth) CreateInitialSession(ctx context.Context, resolved *identity.Identity, appPassword string) (*comatproto.ServerCreateSession_Output, error) {
+27
appview/resolver.go
+27
appview/resolver.go
···
···
1
+
package appview
2
+
3
+
import (
4
+
"context"
5
+
6
+
"github.com/bluesky-social/indigo/atproto/identity"
7
+
"github.com/bluesky-social/indigo/atproto/syntax"
8
+
)
9
+
10
+
type Resolver struct {
11
+
directory identity.Directory
12
+
}
13
+
14
+
func NewResolver() *Resolver {
15
+
return &Resolver{
16
+
directory: identity.DefaultDirectory(),
17
+
}
18
+
}
19
+
20
+
func (r *Resolver) ResolveIdent(ctx context.Context, arg string) (*identity.Identity, error) {
21
+
id, err := syntax.ParseAtIdentifier(arg)
22
+
if err != nil {
23
+
return nil, err
24
+
}
25
+
26
+
return r.directory.Lookup(ctx, *id)
27
+
}
+20
-14
appview/state/middleware.go
+20
-14
appview/state/middleware.go
···
113
})
114
}
115
116
-
func ResolveIdent(next http.Handler) http.Handler {
117
-
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
118
-
didOrHandle := chi.URLParam(req, "user")
119
120
-
log.Println(didOrHandle)
121
-
id, err := auth.ResolveIdent(req.Context(), didOrHandle)
122
-
if err != nil {
123
-
// invalid did or handle
124
-
log.Println("failed to resolve did/handle")
125
-
w.WriteHeader(http.StatusNotFound)
126
-
return
127
-
}
128
129
-
ctx := context.WithValue(req.Context(), "resolvedId", *id)
130
-
next.ServeHTTP(w, req.WithContext(ctx))
131
-
})
132
}
133
134
func ResolveRepoKnot(s *State) Middleware {
···
113
})
114
}
115
116
+
func ResolveIdent(s *State) Middleware {
117
+
return func(next http.Handler) http.Handler {
118
+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
119
+
start := time.Now()
120
+
didOrHandle := chi.URLParam(req, "user")
121
122
+
log.Println(didOrHandle)
123
+
id, err := s.resolver.ResolveIdent(req.Context(), didOrHandle)
124
+
if err != nil {
125
+
// invalid did or handle
126
+
log.Println("failed to resolve did/handle")
127
+
w.WriteHeader(http.StatusNotFound)
128
+
return
129
+
}
130
+
131
+
ctx := context.WithValue(req.Context(), "resolvedId", *id)
132
133
+
elapsed := time.Since(start)
134
+
log.Println("Execution time:", elapsed)
135
+
next.ServeHTTP(w, req.WithContext(ctx))
136
+
})
137
+
}
138
}
139
140
func ResolveRepoKnot(s *State) Middleware {
+14
-6
appview/state/state.go
+14
-6
appview/state/state.go
···
30
enforcer *rbac.Enforcer
31
tidClock *syntax.TIDClock
32
pages *pages.Pages
33
}
34
35
func Make() (*State, error) {
···
52
53
pgs := pages.NewPages()
54
55
-
return &State{db, auth, enforcer, clock, pgs}, nil
56
}
57
58
func (s *State) TID() string {
···
76
fmt.Println("handle", handle)
77
fmt.Println("app_password", appPassword)
78
79
-
resolved, err := auth.ResolveIdent(ctx, handle)
80
if err != nil {
81
log.Printf("resolving identity: %s", err)
82
http.Redirect(w, r, "/login", http.StatusSeeOther)
···
169
return
170
}
171
172
-
id, err := auth.ResolveIdent(r.Context(), user)
173
if err != nil {
174
w.WriteHeader(http.StatusInternalServerError)
175
return
···
415
return
416
}
417
418
-
memberIdent, err := auth.ResolveIdent(r.Context(), memberDid)
419
if err != nil {
420
w.Write([]byte("failed to resolve member did to a handle"))
421
return
···
556
return
557
}
558
559
-
ident, err := auth.ResolveIdent(r.Context(), didOrHandle)
560
if err != nil {
561
log.Printf("resolving identity: %s", err)
562
w.WriteHeader(http.StatusNotFound)
···
597
// strip @ from user
598
r.Use(StripLeadingAt)
599
600
-
r.With(ResolveIdent).Route("/{user}", func(r chi.Router) {
601
r.Get("/", s.ProfilePage)
602
r.With(ResolveRepoKnot(s)).Route("/{repo}", func(r chi.Router) {
603
r.Get("/", s.RepoIndex)
···
30
enforcer *rbac.Enforcer
31
tidClock *syntax.TIDClock
32
pages *pages.Pages
33
+
resolver *appview.Resolver
34
}
35
36
func Make() (*State, error) {
···
53
54
pgs := pages.NewPages()
55
56
+
resolver := appview.NewResolver()
57
+
58
+
state := &State{
59
+
db,
60
+
auth, enforcer, clock, pgs, resolver,
61
+
}
62
+
63
+
return state, nil
64
}
65
66
func (s *State) TID() string {
···
84
fmt.Println("handle", handle)
85
fmt.Println("app_password", appPassword)
86
87
+
resolved, err := s.resolver.ResolveIdent(ctx, handle)
88
if err != nil {
89
log.Printf("resolving identity: %s", err)
90
http.Redirect(w, r, "/login", http.StatusSeeOther)
···
177
return
178
}
179
180
+
id, err := s.resolver.ResolveIdent(r.Context(), user)
181
if err != nil {
182
w.WriteHeader(http.StatusInternalServerError)
183
return
···
423
return
424
}
425
426
+
memberIdent, err := s.resolver.ResolveIdent(r.Context(), memberDid)
427
if err != nil {
428
w.Write([]byte("failed to resolve member did to a handle"))
429
return
···
564
return
565
}
566
567
+
ident, err := s.resolver.ResolveIdent(r.Context(), didOrHandle)
568
if err != nil {
569
log.Printf("resolving identity: %s", err)
570
w.WriteHeader(http.StatusNotFound)
···
605
// strip @ from user
606
r.Use(StripLeadingAt)
607
608
+
r.With(ResolveIdent(s)).Route("/{user}", func(r chi.Router) {
609
r.Get("/", s.ProfilePage)
610
r.With(ResolveRepoKnot(s)).Route("/{repo}", func(r chi.Router) {
611
r.Get("/", s.RepoIndex)
+3
-17
cmd/repoguard/main.go
+3
-17
cmd/repoguard/main.go
···
9
"net/url"
10
"os"
11
"os/exec"
12
-
"path"
13
"path/filepath"
14
"strings"
15
"time"
16
17
-
"github.com/sotangled/tangled/appview/auth"
18
)
19
20
var (
···
136
}
137
138
func resolveToDid(didOrHandle string) string {
139
-
ident, err := auth.ResolveIdent(context.Background(), didOrHandle)
140
if err != nil {
141
exitWithLog(fmt.Sprintf("error resolving handle: %v", err))
142
}
143
144
// did:plc:foobarbaz/repo
145
return ident.DID.String()
146
-
}
147
-
148
-
func handleToDid(handlePath string) string {
149
-
handle := path.Dir(handlePath)
150
-
151
-
ident, err := auth.ResolveIdent(context.Background(), handle)
152
-
if err != nil {
153
-
exitWithLog(fmt.Sprintf("error resolving handle: %v", err))
154
-
}
155
-
156
-
// did:plc:foobarbaz/repo
157
-
didPath := filepath.Join(ident.DID.String(), path.Base(handlePath))
158
-
159
-
return didPath
160
}
161
162
func initLogger() {
···
9
"net/url"
10
"os"
11
"os/exec"
12
"path/filepath"
13
"strings"
14
"time"
15
16
+
"github.com/sotangled/tangled/appview"
17
)
18
19
var (
···
135
}
136
137
func resolveToDid(didOrHandle string) string {
138
+
resolver := appview.NewResolver()
139
+
ident, err := resolver.ResolveIdent(context.Background(), didOrHandle)
140
if err != nil {
141
exitWithLog(fmt.Sprintf("error resolving handle: %v", err))
142
}
143
144
// did:plc:foobarbaz/repo
145
return ident.DID.String()
146
}
147
148
func initLogger() {
+2
-2
knotserver/file.go
+2
-2
knotserver/file.go