auth_basic.go 824 B

123456789101112131415161718192021222324252627282930313233
  1. package upstreamproxy
  2. import (
  3. "errors"
  4. "net/http"
  5. )
  6. type BasicHttpAuthState int
  7. const (
  8. BASIC_HTTP_AUTH_STATE_CHALLENGE_RECEIVED BasicHttpAuthState = iota
  9. BASIC_HTTP_AUTH_STATE_RESPONSE_GENERATED
  10. )
  11. type BasicHttpAuthenticator struct {
  12. state BasicHttpAuthState
  13. challenge string
  14. }
  15. func newBasicAuthenticator(challenge string) *BasicHttpAuthenticator {
  16. return &BasicHttpAuthenticator{state: BASIC_HTTP_AUTH_STATE_CHALLENGE_RECEIVED,
  17. challenge: challenge}
  18. }
  19. func (a BasicHttpAuthenticator) authenticate(req *http.Request, username, password string) error {
  20. if a.state == BASIC_HTTP_AUTH_STATE_CHALLENGE_RECEIVED {
  21. req.SetBasicAuth(username, password)
  22. a.state = BASIC_HTTP_AUTH_STATE_RESPONSE_GENERATED
  23. return nil
  24. } else {
  25. return errors.New("Authorization is not accepted by the proxy server")
  26. }
  27. }