github.com/cs3org/reva/v2@v2.27.7/internal/http/interceptors/auth/credential/strategy/bearer/bearer.go (about) 1 // Copyright 2018-2021 CERN 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package bearer 20 21 import ( 22 "fmt" 23 "net/http" 24 "strings" 25 26 "github.com/cs3org/reva/v2/internal/http/interceptors/auth/credential/registry" 27 "github.com/cs3org/reva/v2/pkg/auth" 28 ) 29 30 func init() { 31 registry.Register("bearer", New) 32 } 33 34 type strategy struct{} 35 36 // New returns a new auth strategy that checks "Bearer" OAuth Access Tokens 37 // See https://tools.ietf.org/html/rfc6750#section-6.1 38 func New(m map[string]interface{}) (auth.CredentialStrategy, error) { 39 return &strategy{}, nil 40 } 41 42 func (s *strategy) GetCredentials(w http.ResponseWriter, r *http.Request) (*auth.Credentials, error) { 43 // 1. check Authorization header 44 hdr := r.Header.Get("Authorization") 45 token := strings.TrimPrefix(hdr, "Bearer ") 46 if token != "" { 47 return &auth.Credentials{Type: "bearer", ClientSecret: token}, nil 48 } 49 // TODO 2. check form encoded body parameter for POST requests, see https://tools.ietf.org/html/rfc6750#section-2.2 50 51 // 3. check uri query parameter, see https://tools.ietf.org/html/rfc6750#section-2.3 52 tokens, ok := r.URL.Query()["access_token"] 53 if !ok || len(tokens[0]) < 1 { 54 return nil, fmt.Errorf("no bearer auth provided") 55 } 56 return &auth.Credentials{Type: "bearer", ClientSecret: tokens[0]}, nil 57 58 } 59 60 func (s *strategy) AddWWWAuthenticate(w http.ResponseWriter, r *http.Request, realm string) { 61 // TODO read realm from forwarded header? 62 if realm == "" { 63 // fall back to hostname if not configured 64 realm = r.Host 65 } 66 w.Header().Add("WWW-Authenticate", fmt.Sprintf(`Bearer realm="%s"`, realm)) 67 }