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

Set basic HTTP authentification #15

Closed
montmejat opened this issue Feb 10, 2023 · 4 comments
Closed

Set basic HTTP authentification #15

montmejat opened this issue Feb 10, 2023 · 4 comments

Comments

@montmejat
Copy link

montmejat commented Feb 10, 2023

First of all, thanks for this simple and great tool. I need to access a WMS server that has basic HTTP authentication. I had to add an extra line in the request function from wms/client.go to make it work:

func (c *Client) request(ctx context.Context, method string, url string, timeout int) ([]byte, error) {
	ctx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Millisecond)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, method, url, nil)
	req.SetBasicAuth("username", "password") // <-- what I needed to add
	if err != nil {
		return nil, err
	}

	res, err := c.httpClient.Do(req)
	if err != nil {
		return nil, err
	}

	if res.StatusCode >= 400 || res.StatusCode < 200 {
		return nil, fmt.Errorf("error making HTTP request (%v): %s", res.StatusCode, http.StatusText(res.StatusCode))
	}

	resBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	return resBody, nil
}

It would be nice to have an optional argument passing a username and password for this case, unless you see any better way of doing it? I can propose a PR for this. 😄

@lmikolajczak
Copy link
Owner

Hi @aurelien-m 👋

Thanks for opening this issue! I'm open to any proposals you have to fix this, we can briefly discuss it and either you can open a PR with a fix or I can work on it. Up to you 😉 Let me know which way you prefer.

@lmikolajczak
Copy link
Owner

Speaking of possible solution, the first and most simple thing that comes to my mind is to add new ClientOption:

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

Then we can access both username and password in the request method of the Client via c.username, c.password and use it to set it on the request object as you did in the code snippet above 😉 What do you think?

@montmejat
Copy link
Author

Hey @lmikolajczak!

So I've actually done something similar in my own fork: https://github.com/aurelien-m/wms-tiles-downloader/pull/1/files. I've also added useBasicAuth in the client struct to check if we need authentication or not. I can create a draft pull request out of it if you think it's a good start.

And by the way, I haven't done a lot of Go, so don't hesitate if you see anything clumsy 😄

@montmejat
Copy link
Author

Extra note: I've also modify your app to be able to read GeoJSON files and extract tiles from a polygon. You can see what I've done here: https://github.com/aurelien-m/wms-tiles-downloader/pull/2/files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants