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

Basic auth #1

Merged
merged 3 commits into from
Feb 15, 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
26 changes: 25 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"context"
"fmt"
"strings"

"github.com/lmikolajczak/wms-tiles-downloader/mercantile"
"github.com/lmikolajczak/wms-tiles-downloader/wms"
"github.com/schollz/progressbar/v3"
Expand Down Expand Up @@ -40,7 +42,26 @@ var getCmd = &cobra.Command{
if err != nil {
fmt.Printf("ERR: %s\n", err)
}
WMSClient, err := wms.NewClient(url, wms.WithQueryString(params), wms.WithVersion(version))
auth, err := cmd.Flags().GetString("auth")
if err != nil {
fmt.Printf("ERR: %s\n", err)
}
username := ""
password := ""
if auth != "" {
if !strings.Contains(auth, ":") {
fmt.Printf("ERR: %s\n", "Invalid auth format. Should be username:password")
} else {
username = auth[:strings.Index(auth, ":")]
password = auth[strings.Index(auth, ":")+1:]
}
}
WMSClient, err := wms.NewClient(
url,
wms.WithQueryString(params),
wms.WithVersion(version),
wms.WithBasicAuth(username, password),
)
if err != nil {
fmt.Printf("ERR: %s\n", err)
}
Expand Down Expand Up @@ -165,4 +186,7 @@ func init() {
getCmd.Flags().StringToString(
"params", nil, "Custom query string params",
)
getCmd.Flags().String(
"auth", "", "Basic auth credentials in the form of username:password",
)
}
18 changes: 17 additions & 1 deletion wms/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/lmikolajczak/wms-tiles-downloader/mercantile"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"time"

"github.com/lmikolajczak/wms-tiles-downloader/mercantile"
)

const (
Expand All @@ -28,6 +29,9 @@ type Client struct {
requestType string
spatialRefSystem string
queryStrings map[string]string
useBasicAuth bool
username string
password string
}

type ClientOption func(c *Client)
Expand All @@ -50,6 +54,14 @@ func WithQueryString(qs map[string]string) ClientOption {
}
}

func WithBasicAuth(username, password string) ClientOption {
return func(c *Client) {
c.username = username
c.password = password
c.useBasicAuth = true
}
}

func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
if baseURL == "" {
return nil, errors.New("baseURL is required")
Expand All @@ -62,6 +74,7 @@ func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
service: "WMS",
requestType: "GetMap",
spatialRefSystem: "EPSG:3857",
useBasicAuth: false,
}

for _, option := range options {
Expand Down Expand Up @@ -136,6 +149,9 @@ func (c *Client) request(ctx context.Context, method string, url string, timeout
defer cancel()

req, err := http.NewRequestWithContext(ctx, method, url, nil)
if c.useBasicAuth {
req.SetBasicAuth(c.username, c.password)
}
if err != nil {
return nil, err
}
Expand Down