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: compatible with multiaddr #194

Merged
merged 2 commits into from
Nov 28, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/ipfs-force-community/metrics v1.0.1-0.20231011024528-8c881d456601
github.com/ipfs/go-log/v2 v2.5.1
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.8.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.3
Expand Down Expand Up @@ -90,7 +91,6 @@ require (
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/multiformats/go-multiaddr v0.8.0 // indirect
github.com/multiformats/go-multibase v0.1.1 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
Expand Down
70 changes: 69 additions & 1 deletion jwtclient/auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/go-resty/resty/v2"
"github.com/multiformats/go-multiaddr"
"go.opencensus.io/trace"
"golang.org/x/xerrors"

Expand All @@ -16,6 +18,8 @@ import (
"github.com/ipfs-force-community/sophon-auth/auth"
"github.com/ipfs-force-community/sophon-auth/core"
"github.com/ipfs-force-community/sophon-auth/errcode"

maNet "github.com/multiformats/go-multiaddr/net"
)

//go:generate mockgen -destination=mocks/mock_auth_client.go -package=mocks github.com/ipfs-force-community/sophon-auth/jwtclient IAuthClient
Expand Down Expand Up @@ -50,10 +54,14 @@ type AuthClient struct {
cli *resty.Client
}

func NewAuthClient(url string, token string) (*AuthClient, error) {
func NewAuthClient(addr string, token string) (*AuthClient, error) {
if len(token) == 0 {
return nil, xerrors.Errorf("token is empty")
}
url, err := ParseAddr(addr)
if err != nil {
return nil, fmt.Errorf("parse addr %s: %w", addr, err)
}
client := resty.New().
SetHostURL(url).
SetHeader("Accept", "application/json").
Expand Down Expand Up @@ -533,3 +541,63 @@ func (lc *AuthClient) GetUserBySigner(ctx context.Context, signer address.Addres
}
return nil, resp.Error().(*errcode.ErrMsg).Err()
}

// ParseAddr parse a multi addr to a traditional url ( with http scheme as default)
func ParseAddr(addr string) (string, error) {
ret := addr
ma, err := multiaddr.NewMultiaddr(addr)
if err == nil {
_, addr, err := maNet.DialArgs(ma)
if err != nil {
return "", fmt.Errorf("parser libp2p url fail %w", err)
}

ret = "http://" + addr

_, err = ma.ValueForProtocol(multiaddr.P_WSS)
if err == nil {
ret = "wss://" + addr
} else if err != multiaddr.ErrProtocolNotFound {
return "", err
}

_, err = ma.ValueForProtocol(multiaddr.P_HTTPS)
if err == nil {
ret = "https://" + addr
} else if err != multiaddr.ErrProtocolNotFound {
return "", err
}

_, err = ma.ValueForProtocol(multiaddr.P_WS)
if err == nil {
ret = "ws://" + addr
} else if err != multiaddr.ErrProtocolNotFound {
return "", err
}

_, err = ma.ValueForProtocol(multiaddr.P_HTTP)
if err == nil {
ret = "http://" + addr
} else if err != multiaddr.ErrProtocolNotFound {
return "", err
}
}

u, err := url.Parse(ret)
if err != nil {
return "", fmt.Errorf("parser address fail %w", err)
}
switch u.Scheme {
case "ws":
u.Scheme = "http"
case "wss":
u.Scheme = "https"
case "":
u.Scheme = "https"
case "http", "https":
default:
return "", fmt.Errorf("invalid scheme %s", u.Scheme)
}

return u.String(), nil
}
54 changes: 54 additions & 0 deletions jwtclient/auth_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,57 @@ func TestJWTClient_ListUsers(t *testing.T) {
t.Log(v)
}
}

func TestParseAddr(t *testing.T) {
testCase := []struct {
Input string
Expected string
}{
{
"http://example.com",
"http://example.com",
},
{
"example.com",
"https://example.com",
},
{
"https://127.0.0.1",
"https://127.0.0.1",
},
{
"/dns/example.com/tcp/88",
"http://example.com:88",
},
{
"/dns/example.com/tcp/88/https",
"https://example.com:88",
},
{
"/dns/example.com/tcp/88/wss",
"https://example.com:88",
},
{
"/dns/example.com/tcp/88/ws",
"http://example.com:88",
},
{
"/ip4/127.0.0.1/tcp/88",
"http://127.0.0.1:88",
},
{
"ftp://example.com",
"",
},
}
for _, v := range testCase {
if v.Expected == "" {
_, err := ParseAddr(v.Input)
assert.Error(t, err)
} else {
addr, err := ParseAddr(v.Input)
assert.NoError(t, err)
assert.Equal(t, v.Expected, addr)
}
}
}
2 changes: 1 addition & 1 deletion jwtclient/mocks/mock_auth_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading