commit e0d7f1074b77c0bb3bdd751f878c1e307da9ae69
parent 4d748545d8acd82b070eb63f7e3e333d7d588173
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 28 Dec 2023 10:03:17 +0900
change Auth function
Diffstat:
4 files changed, 29 insertions(+), 67 deletions(-)
diff --git a/auth_test.go b/auth_test.go
@@ -17,32 +17,24 @@ func TestAuth(t *testing.T) {
defer func() {
acr.Close()
acw.Close()
- asr.Close()
- asw.Close()
}()
- conn.S.Auth = func(ctx context.Context, authc <-chan *lib9p.Req, respc chan<- *lib9p.Req) {
- for {
- select {
- case <-ctx.Done():
- return
- case r, ok := <-authc:
- if !ok {
- return
- }
- ifcall := r.Ifcall.(*lib9p.TAuth)
- aqid := lib9p.Qid{Type: lib9p.QTAUTH, Vers: 0, Path: ^uint64(0)}
- r.Afid.File = &lib9p.AuthFile{
- Qid: aqid,
- Uname: ifcall.Uname,
- Aname: ifcall.Aname,
- W: acw,
- R: acr,
- }
- runAuth(ctx, t, r.Afid.File.(*lib9p.AuthFile), asr, asw)
- r.Ofcall = &lib9p.RAuth{Tag: ifcall.Tag, Aqid: aqid}
- respc <- r
- }
+ conn.S.Auth = func(ctx context.Context, r *lib9p.Req) {
+ ifcall := r.Ifcall.(*lib9p.TAuth)
+ aqid := lib9p.Qid{Type: lib9p.QTAUTH, Vers: 0, Path: ^uint64(0)}
+ r.Afid.File = &lib9p.AuthFile{
+ Qid: aqid,
+ Uname: ifcall.Uname,
+ Aname: ifcall.Aname,
+ W: acw,
+ R: acr,
}
+ go func() {
+ <-ctx.Done()
+ asr.Close()
+ asw.Close()
+ }()
+ runAuth(ctx, t, r.Afid.File.(*lib9p.AuthFile), asr, asw)
+ r.Ofcall = &lib9p.RAuth{Tag: ifcall.Tag, Aqid: aqid}
}
ctx := context.Background()
_, _, err := conn.C.Version(ctx, lib9p.NOTAG, 8*1024, "9P2000")
@@ -80,6 +72,7 @@ func TestAuth(t *testing.T) {
}
// Dumb state machine...
+// TODO: return when ctx is canceled
func runAuth(ctx context.Context, t *testing.T, afile *lib9p.AuthFile, r io.Reader, w io.Writer) {
go func() {
buf := make([]byte, 10)
diff --git a/server.go b/server.go
@@ -64,16 +64,13 @@ type Server struct {
respChan chan *Req
- // Auth function is passed an auth request via authc when a TAuth message
- // arrives. It is called as a separate goroutine and should listen to
- // authc and send processed *Req through respc.
- // Don't close these channels.
- // If authentication is desired, the Auth goroutine should
+ // Auth function is passed an auth request when a TAuth message arrives
+ // If authentication is desired, Auth should
// set Req.Afid.File to an *AuthFile and Req.Ofcall.Qid, and prepare to
// authenticate via the Read()/Write() calls to Req.Afid.File.
- // Auth goroutine should return when ctx is canceled.
+ // Auth should clean up everything it creates when ctx is canceled.
// If this is nil, no authentication is performed.
- Auth func(ctx context.Context, authc <-chan *Req, respc chan<- *Req)
+ Auth func(ctx context.Context, r *Req)
}
// NewServer creates a Server and runs listener and speaker goroutines.
@@ -236,12 +233,6 @@ func sVersion(ctx context.Context, s *Server, c <-chan *Req) {
// sAuth serves Tauth message.
func sAuth(ctx context.Context, s *Server, c <-chan *Req) {
- var authc chan *Req
- if s.Auth != nil {
- authc = make(chan *Req)
- go s.Auth(ctx, authc, s.respChan)
- defer close(authc)
- }
for {
select {
case <-ctx.Done():
@@ -250,7 +241,7 @@ func sAuth(ctx context.Context, s *Server, c <-chan *Req) {
if !ok {
return
}
- if authc == nil {
+ if s.Auth == nil {
setError(r, fmt.Errorf("authentication not required"))
select {
case s.respChan <- r:
@@ -280,7 +271,8 @@ func sAuth(ctx context.Context, s *Server, c <-chan *Req) {
}
continue
}
- authc <- r // TODO: I think Req.listenErr should be exported.
+ s.Auth(ctx, r)
+ s.respChan <- r
}
}
}
diff --git a/server2_test.go b/server2_test.go
@@ -178,40 +178,17 @@ func TestSAuth(t *testing.T) {
tests := []struct {
input *Req
want *Req
- authFunc func(context.Context, <-chan *Req, chan<- *Req)
+ authFunc func(context.Context, *Req)
}{
{&Req{Ifcall: &TAuth{Afid: NOFID, Uname: "kenji", Aname: ""}},
&Req{Ofcall: &RError{Ename: errors.New("authentication not required")}}, nil},
{&Req{Ifcall: &TAuth{Afid: NOFID, Uname: "kenji", Aname: ""}},
&Req{Ofcall: &RError{Ename: errors.New("NOFID can't be used for afid")}},
- func(ctx context.Context, authc <-chan *Req, respc chan<- *Req) {
- for {
- select {
- case r, ok := <-authc:
- if !ok {
- return
- }
- respc <- r
- case <-ctx.Done():
- return
- }
- }
- }},
+ func(ctx context.Context, r *Req) {}},
{&Req{Ifcall: &TAuth{Afid: 0, Uname: "kenji", Aname: ""}},
&Req{Ofcall: &RAuth{Tag: 0, Aqid: Qid{0, 1, 2}}},
- func(ctx context.Context, authc <-chan *Req, respc chan<- *Req) {
- for {
- select {
- case r, ok := <-authc:
- if !ok {
- return
- }
- r.Ofcall = &RAuth{Tag: 0, Aqid: Qid{0, 1, 2}}
- respc <- r
- case <-ctx.Done():
- return
- }
- }
+ func(ctx context.Context, r *Req) {
+ r.Ofcall = &RAuth{Tag: 0, Aqid: Qid{0, 1, 2}}
}},
}
for _, test := range tests {
diff --git a/server_test.go b/server_test.go
@@ -12,7 +12,7 @@ import (
)
func TestSAttach(t *testing.T) {
- dammyAuth := func(context.Context, <-chan *lib9p.Req, chan<- *lib9p.Req) {}
+ dammyAuth := func(context.Context, *lib9p.Req) {}
af := &lib9p.AuthFile{
Qid: lib9p.Qid{Type: lib9p.QTAUTH},
Uname: "kenji",