github.com/argoproj/argo-events@v1.9.1/eventsources/sources/github/tokenauth.go (about) 1 /* 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package github 15 16 import "net/http" 17 18 type TokenAuthStrategy struct { 19 Token string 20 Transport http.RoundTripper 21 } 22 23 // RoundTrip implements the RoundTripper interface. 24 func (t *TokenAuthStrategy) RoundTrip(req *http.Request) (*http.Response, error) { 25 // To set extra headers, we must make a copy of the Request so 26 // that we don't modify the Request we were given. This is required by the 27 // specification of http.RoundTripper. 28 // 29 // Since we are going to modify only req.Header here, we only need a deep copy 30 // of req.Header. 31 req2 := new(http.Request) 32 *req2 = *req 33 req2.Header = make(http.Header, len(req.Header)) 34 for k, s := range req.Header { 35 req2.Header[k] = append([]string(nil), s...) 36 } 37 req2.Header.Add("Authorization", "token "+t.Token) 38 39 return t.transport().RoundTrip(req2) 40 } 41 42 // AuthTransport implements the AuthStrategy interface. 43 func (t *TokenAuthStrategy) AuthTransport() (http.RoundTripper, error) { 44 return t, nil 45 } 46 47 func (t *TokenAuthStrategy) transport() http.RoundTripper { 48 if t.Transport != nil { 49 return t.Transport 50 } 51 52 return http.DefaultTransport 53 }