|
@@ -1,17 +1,29 @@
|
|
|
package url
|
|
|
|
|
|
import (
|
|
|
+ "git.nspix.com/golang/micro/helper/tokenize"
|
|
|
"net/url"
|
|
|
"os"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ AuthFormField = "access_token"
|
|
|
+ AuthMethodField = "auth_method"
|
|
|
+ AuthMethodGateway = "Sugo"
|
|
|
)
|
|
|
|
|
|
type (
|
|
|
Options struct {
|
|
|
- Schema string
|
|
|
- Host string
|
|
|
- Path string // encoded query values, without '?'
|
|
|
- Fragment string // fragment for references, without '#'
|
|
|
- Params map[string]string
|
|
|
+ Schema string
|
|
|
+ Host string
|
|
|
+ Path string // encoded query values, without '?'
|
|
|
+ Fragment string // fragment for references, without '#'
|
|
|
+ Params map[string]string
|
|
|
+ EnableAuth bool //enable auth
|
|
|
+ AuthAppName string //auth app name
|
|
|
+ AuthUserID string
|
|
|
+ AuthExpired time.Duration
|
|
|
}
|
|
|
Option func(o *Options)
|
|
|
)
|
|
@@ -30,6 +42,15 @@ func WithSchema(schema string) Option {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func WithAuth(app string, uid string, expired time.Duration) Option {
|
|
|
+ return func(o *Options) {
|
|
|
+ o.EnableAuth = true
|
|
|
+ o.AuthAppName = app
|
|
|
+ o.AuthUserID = uid
|
|
|
+ o.AuthExpired = expired
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func WithFragment(fragment string) Option {
|
|
|
return func(o *Options) {
|
|
|
o.Fragment = fragment
|
|
@@ -63,6 +84,12 @@ func Create(opts ...Option) string {
|
|
|
for k, v := range o.Params {
|
|
|
qs.Set(k, v)
|
|
|
}
|
|
|
+ if o.EnableAuth {
|
|
|
+ if token, err := tokenize.Encode(o.AuthAppName, o.AuthUserID, time.Now().Add(o.AuthExpired)); err == nil {
|
|
|
+ qs.Set(AuthFormField, token)
|
|
|
+ qs.Set(AuthMethodField, AuthMethodGateway)
|
|
|
+ }
|
|
|
+ }
|
|
|
uri := url.URL{
|
|
|
Scheme: o.Schema,
|
|
|
Host: o.Host,
|