Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #46 from uc-cdis/fix/wts-token
Browse files Browse the repository at this point in the history
Fix/wts token
  • Loading branch information
cterrazas2 authored Apr 6, 2021
2 parents 1cb8dcf + 9be1c22 commit c6b1ffd
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions mariner/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,13 @@ func (server *Server) setResponseHeader(next http.Handler) http.Handler {
})
}

// auth middleware - processes every request, checks auth with arborist
// if arborist says 'okay', then process the request
// if arborist says 'not okay', then http error 'not authorized'
// handleAuth is invoked by the server to use arborist and wts to authorize user access.
func (server *Server) handleAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if server.authZ(r) {
// fmt.Println("user has access") // log
if server.authZ(r) && server.fetchRefreshToken() {
next.ServeHTTP(w, r)
return
}
// fmt.Println("user does NOT have access") // log
http.Error(w, "user not authorized to access this resource", 403)
})
}
Expand Down Expand Up @@ -516,6 +512,33 @@ func (server *Server) authZ(r *http.Request) bool {
return authResponse.Auth
}

// fetchRefreshToken is invoked from the server to check if a refresh token is expired and fetches a new one if it is.
func (server *Server) fetchRefreshToken() bool {
wtsPath := "http://workspace-token-service/oauth2/"
connectedUrl := wtsPath + "connected"
res, err := http.Get(connectedUrl)
if err != nil {
fmt.Println("error checking if user is connected or has a valid token via wts")
return false
}
if res.StatusCode != 200 {
fmt.Println("refresh token expired or user not logged in, fetching new refresh token")
authUrl := wtsPath + "authorization_url?redirect=/"
res, err := http.Get(authUrl)
if err != nil {
fmt.Println("error fetching refresh token from wts")
return false
}
if res.StatusCode == 400 {
fmt.Println("wts refresh token bad request, user error")
return false
}
res.Body.Close()
}
res.Body.Close()
return true
}

// HandleHealthcheck registers root endpoint
// fixme
func (server *Server) handleHealthCheck(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit c6b1ffd

Please sign in to comment.