github.com/pelicanplatform/pelican@v1.0.5/client/bearer_auth.go (about) 1 /*************************************************************** 2 * 3 * Copyright (C) 2023, University of Nebraska-Lincoln 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you 6 * may not use this file except in compliance with the License. You may 7 * obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ***************************************************************/ 18 package client 19 20 import ( 21 "fmt" 22 "io" 23 "net/http" 24 25 "github.com/studio-b12/gowebdav" 26 ) 27 28 // BasicAuth structure holds our credentials, this is the authorizer 29 type bearerAuth struct { 30 token string 31 } 32 33 // BearerAuthenticator is an Authenticator for BearerAuth 34 type bearerAuthenticator struct { 35 token string 36 } 37 38 // NewAuthenticator creates a new BearerAuthenticator 39 func (b *bearerAuth) NewAuthenticator(body io.Reader) (gowebdav.Authenticator, io.Reader) { 40 return &bearerAuthenticator{token: b.token}, body 41 } 42 43 // AddAuthenticator is not needed in this case (but required to have in gowebdav) 44 func (b *bearerAuth) AddAuthenticator(key string, fn gowebdav.AuthFactory) { 45 // Not needed for BearerAuth 46 } 47 48 // Authorize the current request 49 func (b *bearerAuthenticator) Authorize(c *http.Client, rq *http.Request, path string) error { 50 rq.Header.Add("Authorization", "Bearer "+b.token) //set the header with the token 51 return nil 52 } 53 54 // Verify verifies the authentication 55 func (b *bearerAuthenticator) Verify(c *http.Client, rs *http.Response, path string) (redo bool, err error) { 56 if rs.StatusCode == 401 { 57 err := fmt.Errorf("Authorize: %s, %v", path, rs.StatusCode) 58 return true, err 59 } 60 return 61 } 62 63 // Close cleans up all resources 64 func (b *bearerAuthenticator) Close() error { 65 return nil 66 } 67 68 // Clone creates a Copy of itself 69 func (b *bearerAuthenticator) Clone() gowebdav.Authenticator { 70 // no copy due to read only access 71 return b 72 }