2018-04-07 15:37:48 +02:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
2023-02-03 10:18:35 +01:00
|
|
|
"fmt"
|
2024-01-20 11:27:24 +01:00
|
|
|
"log"
|
2020-08-18 04:47:58 +02:00
|
|
|
"net/http"
|
2018-04-07 15:37:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// BasicAuth structure holds our credentials
|
|
|
|
type BasicAuth struct {
|
|
|
|
user string
|
|
|
|
pw string
|
|
|
|
}
|
|
|
|
|
2024-01-20 12:18:22 +01:00
|
|
|
// NewDigestAuth creates a new instance of our Digest Authenticator
|
|
|
|
func NewBasicAuth(login, secret string) (Authenticator, error) {
|
|
|
|
return &BasicAuth{user: login, pw: secret}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
// Authorize the current request
|
|
|
|
func (b *BasicAuth) Authorize(c *http.Client, rq *http.Request, path string) error {
|
|
|
|
rq.SetBasicAuth(b.user, b.pw)
|
2024-01-20 11:27:24 +01:00
|
|
|
log.Printf("BasicAuth.Authorize : SetBasicAuth(%s, %s)", b.user, b.pw)
|
2023-02-03 10:18:35 +01:00
|
|
|
return nil
|
2018-04-07 15:37:48 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
// Verify verifies if the authentication
|
|
|
|
func (b *BasicAuth) Verify(c *http.Client, rs *http.Response, path string) (redo bool, err error) {
|
2024-01-20 11:27:24 +01:00
|
|
|
log.Printf("BasicAuth.Verify : StatusCode = %d", rs.StatusCode)
|
2023-02-03 10:18:35 +01:00
|
|
|
if rs.StatusCode == 401 {
|
|
|
|
err = NewPathError("Authorize", path, rs.StatusCode)
|
|
|
|
}
|
|
|
|
return
|
2018-04-07 15:37:48 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
// Close cleans up all resources
|
|
|
|
func (b *BasicAuth) Close() error {
|
2024-01-20 11:27:24 +01:00
|
|
|
log.Printf("BasicAuth.Close")
|
2023-02-03 10:18:35 +01:00
|
|
|
return nil
|
2018-04-07 15:37:48 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
// Clone creates a Copy of itself
|
|
|
|
func (b *BasicAuth) Clone() Authenticator {
|
2024-01-20 11:27:24 +01:00
|
|
|
log.Printf("BasicAuth.Clone")
|
2023-02-03 10:18:35 +01:00
|
|
|
// no copy due to read only access
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// String toString
|
|
|
|
func (b *BasicAuth) String() string {
|
2024-01-20 11:27:24 +01:00
|
|
|
log.Printf("BasicAuth.String")
|
2023-02-03 10:18:35 +01:00
|
|
|
return fmt.Sprintf("BasicAuth login: %s", b.user)
|
2018-04-07 15:37:48 +02:00
|
|
|
}
|