66 "io"
77 "net/http"
88 "path"
9- "sync"
109
1110 "github.com/RangelReale/osincli"
1211
@@ -21,17 +20,9 @@ import (
2120
2221type tokenRequest struct {
2322 publicMasterURL string
24- // osinOAuthClient is the private OAuth client used by this endpoint.
25- // It starts out nil and is lazily initialized when this endpoint is called.
26- osinOAuthClient * osincli.Client
2723 // osinOAuthClientGetter is used to initialize osinOAuthClient.
2824 // Since it can return an error, it may be called multiple times.
2925 osinOAuthClientGetter func () (* osincli.Client , error )
30- // ready is closed to signal that osinOAuthClient is no longer nil.
31- // Nothing sends on ready so <-ready only returns when it has been closed.
32- ready chan struct {}
33- // initLock guards reads and writes to osinOAuthClient when it could still be nil.
34- initLock sync.Mutex
3526
3627 // to check if we need the logout link for the bootstrap user
3728 tokens v1.OAuthAccessTokenInterface
@@ -42,66 +33,43 @@ func NewTokenRequest(publicMasterURL, openShiftLogoutPrefix string, osinOAuthCli
4233 return & tokenRequest {
4334 publicMasterURL : publicMasterURL ,
4435 osinOAuthClientGetter : osinOAuthClientGetter ,
45- ready : make (chan struct {}),
4636 tokens : tokens ,
4737 openShiftLogoutPrefix : openShiftLogoutPrefix ,
4838 }
4939}
5040
5141func (t * tokenRequest ) Install (mux oauthserver.Mux , prefix string ) {
52- mux .HandleFunc (path .Join (prefix , urls .RequestTokenEndpoint ), t .readyHandler (t .requestToken ))
53- mux .HandleFunc (path .Join (prefix , urls .DisplayTokenEndpoint ), t .readyHandler (t .displayToken ))
42+ mux .HandleFunc (path .Join (prefix , urls .RequestTokenEndpoint ), t .oauthClientHandler (t .requestToken ))
43+ mux .HandleFunc (path .Join (prefix , urls .DisplayTokenEndpoint ), t .oauthClientHandler (t .displayToken ))
5444 mux .HandleFunc (path .Join (prefix , urls .ImplicitTokenEndpoint ), t .implicitToken )
5545}
5646
57- // TODO we may want to start doing live lookups for this endpoint
58- func (t * tokenRequest ) readyHandler (delegate func (http.ResponseWriter , * http.Request )) func (http.ResponseWriter , * http.Request ) {
47+ func (t * tokenRequest ) oauthClientHandler (delegate func (* osincli.Client , http.ResponseWriter , * http.Request )) func (http.ResponseWriter , * http.Request ) {
5948 return func (w http.ResponseWriter , h * http.Request ) {
60- select {
61- case <- t .ready :
62- default :
63- if err := t .safeInitOsinOAuthClientOnce (); err != nil {
64- utilruntime .HandleError (fmt .Errorf ("failed to get Osin OAuth client for token endpoint: %v" , err ))
65- http .Error (w , "OAuth token endpoint is not ready" , http .StatusInternalServerError )
66- return
67- }
68- }
69- delegate (w , h )
70- }
71- }
72-
73- // safeInitOsinOAuthClientOnce initializes osinOAuthClient exactly once using osinOAuthClientGetter.
74- // It is goroutine safe, reentrant and can be safely called multiple times.
75- func (t * tokenRequest ) safeInitOsinOAuthClientOnce () error {
76- // Use a lock and nil check to make sure we never close endpoints.ready more than once
77- // and that we only try to fetch osinOAuthClient until the first time we are successful
78- t .initLock .Lock ()
79- defer t .initLock .Unlock ()
80- if t .osinOAuthClient == nil {
8149 osinOAuthClient , err := t .osinOAuthClientGetter ()
8250 if err != nil {
83- return err
51+ utilruntime .HandleError (fmt .Errorf ("failed to get Osin OAuth client for token endpoint: %v" , err ))
52+ http .Error (w , "OAuth token endpoint is not ready" , http .StatusInternalServerError )
53+ return
8454 }
85- t .osinOAuthClient = osinOAuthClient
86- close (t .ready )
55+ delegate (osinOAuthClient , w , h )
8756 }
88- return nil
8957}
9058
9159// requestToken works for getting a token in your browser and seeing what your token is
92- func (t * tokenRequest ) requestToken (w http.ResponseWriter , req * http.Request ) {
93- authReq := t . osinOAuthClient .NewAuthorizeRequest (osincli .CODE )
60+ func (t * tokenRequest ) requestToken (osinOAuthClient * osincli. Client , w http.ResponseWriter , req * http.Request ) {
61+ authReq := osinOAuthClient .NewAuthorizeRequest (osincli .CODE )
9462 oauthURL := authReq .GetAuthorizeUrl ()
9563
9664 http .Redirect (w , req , oauthURL .String (), http .StatusFound )
9765}
9866
99- func (t * tokenRequest ) displayToken (w http.ResponseWriter , req * http.Request ) {
67+ func (t * tokenRequest ) displayToken (osinOAuthClient * osincli. Client , w http.ResponseWriter , req * http.Request ) {
10068 w .Header ().Set ("Content-Type" , "text/html; charset=UTF-8" )
10169 requestURL := urls .OpenShiftOAuthTokenRequestURL ("" ) // relative url to token request endpoint
10270 data := tokenData {RequestURL : requestURL , PublicMasterURL : t .publicMasterURL }
10371
104- authorizeReq := t . osinOAuthClient .NewAuthorizeRequest (osincli .CODE )
72+ authorizeReq := osinOAuthClient .NewAuthorizeRequest (osincli .CODE )
10573 authorizeData , err := authorizeReq .HandleRequest (req )
10674 if err != nil {
10775 data .Error = fmt .Sprintf ("Error handling auth request: %v" , err )
@@ -110,7 +78,7 @@ func (t *tokenRequest) displayToken(w http.ResponseWriter, req *http.Request) {
11078 return
11179 }
11280
113- accessReq := t . osinOAuthClient .NewAccessRequest (osincli .AUTHORIZATION_CODE , authorizeData )
81+ accessReq := osinOAuthClient .NewAccessRequest (osincli .AUTHORIZATION_CODE , authorizeData )
11482 accessData , err := accessReq .GetToken ()
11583 if err != nil {
11684 data .Error = fmt .Sprintf ("Error getting token: %v" , err )
0 commit comments