-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oauth): association userID with clientID
- Loading branch information
blancinot
committed
Oct 7, 2019
1 parent
8b6cce6
commit b1695a4
Showing
13 changed files
with
321 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
restful "github.com/emicklei/go-restful" | ||
) | ||
|
||
const bearerPrefix = "Bearer " | ||
|
||
func (api *API) registerOauth(ws *restful.WebService) { | ||
ws. | ||
Route(ws.GET("/oauth/{provider}"). | ||
To(api.oauthAuthenticate). | ||
Doc("Authenticate using oauth token"). | ||
Param(restful.HeaderParameter("Authorization", "Oauth authorization header")). | ||
Produces("application/json"). | ||
Writes(AuthResponse{})) | ||
} | ||
|
||
func (api *API) oauthAuthenticate(request *restful.Request, response *restful.Response) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
// unhandled error | ||
WriteError(err.(error), response) | ||
} | ||
}() | ||
|
||
authHeader := request.HeaderParameter("Authorization") | ||
if !strings.HasPrefix(authHeader, bearerPrefix) { | ||
response.WriteErrorString(http.StatusUnauthorized, "missing bearer prefix") | ||
return | ||
} | ||
|
||
accessToken := authHeader[len(bearerPrefix):] | ||
provider := request.PathParameter("provider") | ||
|
||
baseURL, err := oauthClientIdentityURL(provider) | ||
if err != nil { | ||
response.WriteError(http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
identityResponse, err := http.Get(baseURL + "?access_token=" + accessToken) | ||
if err != nil { | ||
response.WriteError(http.StatusUnauthorized, fmt.Errorf("failed getting client identity by oauth: %s", err.Error())) | ||
return | ||
} | ||
|
||
defer identityResponse.Body.Close() | ||
contents, err := ioutil.ReadAll(identityResponse.Body) | ||
if err != nil { | ||
response.WriteError(http.StatusUnprocessableEntity, fmt.Errorf("failed reading response body: %s", err.Error())) | ||
return | ||
} | ||
|
||
var clientIdentity map[string]interface{} | ||
if err := json.Unmarshal(contents, &clientIdentity); err != nil { | ||
response.WriteError(http.StatusUnprocessableEntity, fmt.Errorf("failed unmarshalling contents: %s", err.Error())) | ||
return | ||
} | ||
|
||
id := safeStringValue(clientIdentity, "id") | ||
if len(id) == 0 { | ||
id = safeStringValue(clientIdentity, "sub") // different between some oauth providers | ||
if len(id) == 0 { | ||
response.WriteErrorString(http.StatusUnprocessableEntity, "client identity given by oauth is unprocessable") | ||
return | ||
} | ||
} | ||
|
||
exp := time.Now().Add(api.TokenDuration) | ||
user, claims, err := api.Authenticator.FindUser(id, provider, exp) | ||
if err != nil { | ||
response.WriteError(http.StatusUnprocessableEntity, fmt.Errorf("associated user not found: %s", err.Error())) | ||
return | ||
} | ||
_, tokenString, err := api.createToken(user, claims) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = api.checkToken(tokenString) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
response.WriteEntity(&AuthResponse{tokenString, claims}) | ||
} | ||
|
||
func safeStringValue(m map[string]interface{}, field string) string { | ||
v, ok := m[field] | ||
if !ok { | ||
return "" | ||
} | ||
return v.(string) | ||
} | ||
|
||
func oauthClientIdentityURL(provider string) (value string, err error) { | ||
urlEnv := strings.ToUpper(provider) + "_USERIDENTITYURL" | ||
value = os.Getenv(urlEnv) | ||
if len(value) == 0 { | ||
err = fmt.Errorf("client identity url given by provider %s is missing, please verify autentigo configuration [%s]", provider, urlEnv) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.