|
| 1 | +package gowebdav |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | +) |
| 10 | + |
| 11 | +// AlgoChangedErr must be thrown from the Verify method |
| 12 | +// to trigger a re-authentication with a new algorithm. |
| 13 | +type AlgoChangedErr struct{} |
| 14 | + |
| 15 | +func (e AlgoChangedErr) Error() string { |
| 16 | + return "AuthChangedErr" |
| 17 | +} |
| 18 | + |
| 19 | +// AuthFactory prototype function to create a new Authenticator |
| 20 | +type AuthFactory func(rq *http.Request, rs *http.Response, method, path string) (auth Authenticator, err error) |
| 21 | + |
| 22 | +// Authorizer a Authenticator factory |
| 23 | +type Authorizer interface { |
| 24 | + NewAuthenticator(body io.Reader) (Authenticator, io.Reader) |
| 25 | + AddAuthenticator(key string, fn AuthFactory) |
| 26 | +} |
| 27 | + |
| 28 | +// Authenticator stub |
| 29 | +type Authenticator interface { |
| 30 | + Authorize(c *http.Client, rq *http.Request, method string, path string) error |
| 31 | + Verify(rq *http.Request, rs *http.Response, method string, path string) (reauth bool, err error) |
| 32 | + Clone() Authenticator |
| 33 | + io.Closer |
| 34 | +} |
| 35 | + |
| 36 | +// authorizer structure holds our Authenticator create functions |
| 37 | +type authorizer struct { |
| 38 | + factories map[string]AuthFactory |
| 39 | + defAuthMux sync.Mutex |
| 40 | + defAuth Authenticator |
| 41 | +} |
| 42 | + |
| 43 | +// authShim structure that wraps the real Authenticator |
| 44 | +type authShim struct { |
| 45 | + factory AuthFactory |
| 46 | + body io.Reader |
| 47 | + auth Authenticator |
| 48 | +} |
| 49 | + |
| 50 | +// nullAuth initializes the whole authentication flow |
| 51 | +type nullAuth struct{} |
| 52 | + |
| 53 | +// NewAutoAuth creates an auto Authenticator factory |
| 54 | +func NewAutoAuth(login string, secret string) Authorizer { |
| 55 | + fmap := make(map[string]AuthFactory) |
| 56 | + az := &authorizer{fmap, sync.Mutex{}, &nullAuth{}} |
| 57 | + |
| 58 | + az.AddAuthenticator("basic", func(rq *http.Request, rs *http.Response, method, path string) (auth Authenticator, err error) { |
| 59 | + return &BasicAuth{login, secret}, nil |
| 60 | + }) |
| 61 | + |
| 62 | + az.AddAuthenticator("digest", func(rq *http.Request, rs *http.Response, method, path string) (auth Authenticator, err error) { |
| 63 | + return &DigestAuth{login, secret, digestParts(rs)}, nil |
| 64 | + }) |
| 65 | + |
| 66 | + return az |
| 67 | +} |
| 68 | + |
| 69 | +// NewAuthenticator creates an Authenticator (Shim) per request |
| 70 | +func (a *authorizer) NewAuthenticator(body io.Reader) (Authenticator, io.Reader) { |
| 71 | + var retryBuf io.Reader = body |
| 72 | + if body != nil { |
| 73 | + // If the authorization fails, we will need to restart reading |
| 74 | + // from the passed body stream. |
| 75 | + // When body is seekable, use seek to reset the streams |
| 76 | + // cursor to the start. |
| 77 | + // Otherwise, copy the stream into a buffer while uploading |
| 78 | + // and use the buffers content on retry. |
| 79 | + if _, ok := retryBuf.(io.Seeker); ok { |
| 80 | + body = io.NopCloser(body) |
| 81 | + } else { |
| 82 | + buff := &bytes.Buffer{} |
| 83 | + retryBuf = buff |
| 84 | + body = io.TeeReader(body, buff) |
| 85 | + } |
| 86 | + } |
| 87 | + a.defAuthMux.Lock() |
| 88 | + defAuth := a.defAuth.Clone() |
| 89 | + a.defAuthMux.Unlock() |
| 90 | + |
| 91 | + return &authShim{a.factory, retryBuf, defAuth}, body |
| 92 | +} |
| 93 | + |
| 94 | +func (a *authorizer) AddAuthenticator(key string, fn AuthFactory) { |
| 95 | + a.factories[key] = fn |
| 96 | +} |
| 97 | + |
| 98 | +// factory creates an Authenticator instance based on the WWW-Authenticate header |
| 99 | +func (a *authorizer) factory(rq *http.Request, rs *http.Response, method, path string) (auth Authenticator, err error) { |
| 100 | + header := strings.ToLower(rs.Header.Get("Www-Authenticate")) |
| 101 | + for k, fn := range a.factories { |
| 102 | + if strings.Contains(header, k) { |
| 103 | + if auth, err = fn(rq, rs, method, path); err != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + if auth == nil { |
| 110 | + return nil, newPathError("NoAuthenticator", path, rs.StatusCode) |
| 111 | + } |
| 112 | + |
| 113 | + a.defAuthMux.Lock() |
| 114 | + a.defAuth = auth |
| 115 | + a.defAuthMux.Unlock() |
| 116 | + |
| 117 | + return auth, nil |
| 118 | +} |
| 119 | + |
| 120 | +// Authorize the current request |
| 121 | +func (s *authShim) Authorize(c *http.Client, rq *http.Request, method string, path string) error { |
| 122 | + if err := s.auth.Authorize(c, rq, method, path); err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + body := s.body |
| 126 | + rq.GetBody = func() (io.ReadCloser, error) { |
| 127 | + if body != nil { |
| 128 | + if sk, ok := body.(io.Seeker); ok { |
| 129 | + if _, err := sk.Seek(0, io.SeekStart); err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + } |
| 133 | + return io.NopCloser(body), nil |
| 134 | + } |
| 135 | + return nil, nil |
| 136 | + } |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +// Verify checks for authentication issues and may trigger a re-authentication. |
| 141 | +// Catches AlgoChangedErr to update the current Authenticator |
| 142 | +func (s *authShim) Verify(rq *http.Request, rs *http.Response, method string, path string) (reauth bool, err error) { |
| 143 | + reauth, err = s.auth.Verify(rq, rs, method, path) |
| 144 | + if err != nil { |
| 145 | + if _, ok := err.(AlgoChangedErr); ok { |
| 146 | + if auth, aerr := s.factory(rq, rs, method, path); aerr == nil { |
| 147 | + s.auth = auth |
| 148 | + return true, nil |
| 149 | + } else { |
| 150 | + err = aerr |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + return |
| 155 | +} |
| 156 | + |
| 157 | +// Close closes all resources |
| 158 | +func (s *authShim) Close() error { |
| 159 | + if s.body != nil { |
| 160 | + if closer, ok := s.body.(io.Closer); ok { |
| 161 | + return closer.Close() |
| 162 | + } |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +// Clone creates a copy of itself |
| 168 | +func (s *authShim) Clone() Authenticator { |
| 169 | + // panic? |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +// String toString |
| 174 | +func (s *authShim) String() string { |
| 175 | + return "AuthShim" |
| 176 | +} |
| 177 | + |
| 178 | +// Authorize the current request |
| 179 | +func (n *nullAuth) Authorize(c *http.Client, rq *http.Request, method string, path string) error { |
| 180 | + return nil |
| 181 | +} |
| 182 | + |
| 183 | +// Verify checks for authentication issues and may trigger a re-authentication |
| 184 | +func (n *nullAuth) Verify(rq *http.Request, rs *http.Response, method string, path string) (reauth bool, err error) { |
| 185 | + return true, AlgoChangedErr{} |
| 186 | +} |
| 187 | + |
| 188 | +// Close closes all resources |
| 189 | +func (n *nullAuth) Close() error { |
| 190 | + return nil |
| 191 | +} |
| 192 | + |
| 193 | +// Clone creates a copy of itself |
| 194 | +func (n *nullAuth) Clone() Authenticator { |
| 195 | + // no copy due to read only access |
| 196 | + return n |
| 197 | +} |
| 198 | + |
| 199 | +// String toString |
| 200 | +func (n *nullAuth) String() string { |
| 201 | + return "NullAuth" |
| 202 | +} |
0 commit comments