Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add regexp option for truth handle #173

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions jwtclient/auth_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jwtclient
import (
"net/http"
"reflect"
"regexp"
"strings"

"github.com/ipfs-force-community/sophon-auth/auth"
Expand All @@ -13,39 +14,72 @@ import (

var log = logging.Logger("auth_client")

type opt struct {
reg *regexp.Regexp
}

type Option func(*opt)

func RegexpOption(reg *regexp.Regexp) Option {
return func(o *opt) {
o.reg = reg
}
}

type trustHandle struct {
http.Handler
reg *regexp.Regexp
}

// AuthMux used with jsonrpc library to verify whether the request is legal
type AuthMux struct {
handler http.Handler
local, remote IJwtAuthClient

trustHandle map[string]http.Handler
trustHandle map[string]trustHandle
}

func NewAuthMux(local, remote IJwtAuthClient, handler http.Handler) *AuthMux {
return &AuthMux{
handler: handler,
local: local,
remote: remote,
trustHandle: make(map[string]http.Handler),
trustHandle: make(map[string]trustHandle),
}
}

// TrustHandle for requests that can be accessed directly
// if 'pattern' with '/' as suffix, 'TrustHandler' treat it as a root path,
// that it's all sub-path will be trusted.
// if 'pattern' have with prefix and 'reg' is not nil, use 'reg' check 'pattern'.
// if 'pattern' have no '/' with suffix,
// only the URI exactly matches the 'pattern' would be treat as trusted.
func (authMux *AuthMux) TrustHandle(pattern string, handler http.Handler) {
authMux.trustHandle[pattern] = handler
func (authMux *AuthMux) TrustHandle(pattern string, handler http.Handler, opts ...Option) {
opt := new(opt)
for _, o := range opts {
o(opt)
}
authMux.trustHandle[pattern] = trustHandle{
Handler: handler,
reg: opt.reg,
}
}

func (authMux *AuthMux) trustedHandler(uri string) http.Handler {
// todo: we don't consider the situation that 'trustHandle' is changed in parallelly,
// cause we assume trusted handler is static after application initialized
for trustedURI, handler := range authMux.trustHandle {
if trustedURI == uri || (trustedURI[len(trustedURI)-1] == '/' && strings.HasPrefix(uri, trustedURI)) {
if trustedURI == uri {
return handler
}
if strings.HasPrefix(uri, trustedURI) {
if trustedURI[len(trustedURI)-1] == '/' {
return handler
}
if handler.reg != nil && handler.reg.MatchString(uri) {
return handler
}
}
}
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions jwtclient/auth_mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package jwtclient

import (
"context"
"net/http"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -32,3 +34,12 @@ func TestIsNil(t *testing.T) {
ac3 := authCli{c: &mockImp{}}
assert.False(t, isNil(ac3.c))
}

func TestTruthHandle(t *testing.T) {
reg := regexp.MustCompile(`/piece/[a-z0-9]*`)
mux := NewAuthMux(nil, nil, nil)
mock := &http.ServeMux{}
mux.TrustHandle("/piece/", mock, RegexpOption(reg))
path := "/piece/xfdfs1fs"
assert.NotNil(t, mux.trustedHandler(path))
}