-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add multiple handlers to profile api
- Loading branch information
Showing
16 changed files
with
529 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2024 Paul Greenberg [email protected] | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package authn | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/greenpau/go-authcrunch/pkg/authn/enums/operator" | ||
"github.com/greenpau/go-authcrunch/pkg/ids" | ||
"github.com/greenpau/go-authcrunch/pkg/requests" | ||
"github.com/greenpau/go-authcrunch/pkg/tagging" | ||
"github.com/greenpau/go-authcrunch/pkg/user" | ||
) | ||
|
||
// AddUserUniSecFactorToken adds U2F token to user identity. | ||
func (p *Portal) AddUserUniSecFactorToken( | ||
ctx context.Context, | ||
w http.ResponseWriter, | ||
r *http.Request, | ||
rr *requests.Request, | ||
parsedUser *user.User, | ||
resp map[string]interface{}, | ||
usr *user.User, | ||
backend ids.IdentityStore, | ||
bodyData map[string]interface{}) error { | ||
|
||
var tokenTitle, tokenDescription string | ||
var tokenLabels []string = []string{} | ||
var tokenTags []tagging.Tag = []tagging.Tag{} | ||
|
||
// Validate inputs. | ||
if v, exists := bodyData["webauthn_register"]; exists { | ||
rr.WebAuthn.Register = v.(string) | ||
} else { | ||
resp["message"] = "Profile API did not find webauthn_register in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if v, exists := bodyData["webauthn_challenge"]; exists { | ||
rr.WebAuthn.Challenge = v.(string) | ||
} else { | ||
resp["message"] = "Profile API did not find webauthn_challenge in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if v, exists := bodyData["title"]; exists { | ||
tokenTitle = v.(string) | ||
} else { | ||
resp["message"] = "Profile API did not find title in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if v, exists := bodyData["description"]; exists { | ||
tokenDescription = v.(string) | ||
} else { | ||
resp["message"] = "Profile API did not find description in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if extractedTokenTags, err := tagging.ExtractTags(bodyData); err == nil { | ||
for _, extractedTokenTag := range extractedTokenTags { | ||
tokenTags = append(tokenTags, *extractedTokenTag) | ||
} | ||
} else { | ||
resp["message"] = "Profile API find malformed tags in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if extractedTokenLabels, err := tagging.ExtractLabels(bodyData); err == nil { | ||
tokenLabels = extractedTokenLabels | ||
} else { | ||
resp["message"] = "Profile API find malformed tags in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
// Validate data. | ||
if !tokenIssuerRegexPattern.MatchString(tokenTitle) { | ||
resp["message"] = "Profile API found non-compliant token title value" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if !tokenDescriptionRegexPattern.MatchString(tokenDescription) && (tokenDescription != "") { | ||
resp["message"] = "Profile API found non-compliant token description value" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
rr.MfaToken.Type = "u2f" | ||
rr.MfaToken.Comment = tokenTitle | ||
rr.MfaToken.Description = tokenDescription | ||
rr.MfaToken.Tags = tokenTags | ||
rr.MfaToken.Labels = tokenLabels | ||
|
||
if err := backend.Request(operator.AddMfaToken, rr); err != nil { | ||
resp["message"] = "Profile API failed to add token to identity store" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
resp["entry"] = "Created" | ||
return handleAPIProfileResponse(w, rr, http.StatusOK, resp) | ||
} |
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,96 @@ | ||
// Copyright 2024 Paul Greenberg [email protected] | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package authn | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/greenpau/go-authcrunch/pkg/authn/enums/operator" | ||
"github.com/greenpau/go-authcrunch/pkg/identity" | ||
"github.com/greenpau/go-authcrunch/pkg/ids" | ||
"github.com/greenpau/go-authcrunch/pkg/requests" | ||
"github.com/greenpau/go-authcrunch/pkg/user" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// FetchUserInfo fetches user identity information. | ||
func (p *Portal) FetchUserInfo( | ||
ctx context.Context, | ||
w http.ResponseWriter, | ||
r *http.Request, | ||
rr *requests.Request, | ||
parsedUser *user.User, | ||
resp map[string]interface{}, | ||
usr *user.User, | ||
backend ids.IdentityStore) error { | ||
|
||
// Data Buckets | ||
entry := make(map[string]interface{}) | ||
|
||
// General Info | ||
err := backend.Request(operator.GetUser, rr) | ||
if err != nil { | ||
resp["message"] = "failed to extract user metadata" | ||
p.logger.Debug( | ||
"failed to extract user metadata", | ||
zap.String("session_id", rr.Upstream.SessionID), | ||
zap.String("request_id", rr.ID), | ||
zap.Error(err), | ||
) | ||
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp) | ||
} | ||
user := rr.Response.Payload.(*identity.User) | ||
entry["metadata"] = user.GetMetadata() | ||
|
||
// User Roles | ||
|
||
entry["roles"] = parsedUser.Claims.Roles | ||
|
||
// Token | ||
|
||
tokenMap := make(map[string]interface{}) | ||
for k, v := range usr.AsMap() { | ||
tokenMap[k] = v | ||
} | ||
tokenMap["authenticated"] = true | ||
if usr.Claims.ExpiresAt > 0 { | ||
tokenMap["expires_at_utc"] = time.Unix(usr.Claims.ExpiresAt, 0).Format(time.UnixDate) | ||
} | ||
if usr.Claims.IssuedAt > 0 { | ||
tokenMap["issued_at_utc"] = time.Unix(usr.Claims.IssuedAt, 0).Format(time.UnixDate) | ||
} | ||
if usr.Claims.NotBefore > 0 { | ||
tokenMap["not_before_utc"] = time.Unix(usr.Claims.NotBefore, 0).Format(time.UnixDate) | ||
} | ||
prettyTokenMap, err := json.MarshalIndent(tokenMap, "", " ") | ||
if err != nil { | ||
p.logger.Debug( | ||
"failed to prettify user token", | ||
zap.String("session_id", rr.Upstream.SessionID), | ||
zap.String("request_id", rr.ID), | ||
zap.Error(err), | ||
) | ||
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp) | ||
} | ||
entry["token"] = string(prettyTokenMap) | ||
|
||
// Finalize | ||
|
||
resp["entry"] = entry | ||
return handleAPIProfileResponse(w, rr, http.StatusOK, resp) | ||
} |
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.