code.gitea.io/gitea@v1.21.7/services/auth/source/oauth2/source_callout.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package oauth2 5 6 import ( 7 "net/http" 8 9 "github.com/markbates/goth" 10 "github.com/markbates/goth/gothic" 11 ) 12 13 // Callout redirects request/response pair to authenticate against the provider 14 func (source *Source) Callout(request *http.Request, response http.ResponseWriter) error { 15 // not sure if goth is thread safe (?) when using multiple providers 16 request.Header.Set(ProviderHeaderKey, source.authSource.Name) 17 18 // don't use the default gothic begin handler to prevent issues when some error occurs 19 // normally the gothic library will write some custom stuff to the response instead of our own nice error page 20 // gothic.BeginAuthHandler(response, request) 21 22 gothRWMutex.RLock() 23 defer gothRWMutex.RUnlock() 24 25 url, err := gothic.GetAuthURL(response, request) 26 if err == nil { 27 http.Redirect(response, request, url, http.StatusTemporaryRedirect) 28 } 29 return err 30 } 31 32 // Callback handles OAuth callback, resolve to a goth user and send back to original url 33 // this will trigger a new authentication request, but because we save it in the session we can use that 34 func (source *Source) Callback(request *http.Request, response http.ResponseWriter) (goth.User, error) { 35 // not sure if goth is thread safe (?) when using multiple providers 36 request.Header.Set(ProviderHeaderKey, source.authSource.Name) 37 38 gothRWMutex.RLock() 39 defer gothRWMutex.RUnlock() 40 41 user, err := gothic.CompleteUserAuth(response, request) 42 if err != nil { 43 return user, err 44 } 45 46 return user, nil 47 }