github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/api_fetch_user_info.go (about) 1 // Copyright 2024 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package authn 16 17 import ( 18 "context" 19 "encoding/json" 20 "net/http" 21 "time" 22 23 "github.com/greenpau/go-authcrunch/pkg/authn/enums/operator" 24 "github.com/greenpau/go-authcrunch/pkg/identity" 25 "github.com/greenpau/go-authcrunch/pkg/ids" 26 "github.com/greenpau/go-authcrunch/pkg/requests" 27 "github.com/greenpau/go-authcrunch/pkg/user" 28 "go.uber.org/zap" 29 ) 30 31 // FetchUserInfo fetches user identity information. 32 func (p *Portal) FetchUserInfo( 33 ctx context.Context, 34 w http.ResponseWriter, 35 r *http.Request, 36 rr *requests.Request, 37 parsedUser *user.User, 38 resp map[string]interface{}, 39 usr *user.User, 40 backend ids.IdentityStore) error { 41 42 // Data Buckets 43 entry := make(map[string]interface{}) 44 45 // General Info 46 err := backend.Request(operator.GetUser, rr) 47 if err != nil { 48 resp["message"] = "failed to extract user metadata" 49 p.logger.Debug( 50 "failed to extract user metadata", 51 zap.String("session_id", rr.Upstream.SessionID), 52 zap.String("request_id", rr.ID), 53 zap.Error(err), 54 ) 55 return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp) 56 } 57 user := rr.Response.Payload.(*identity.User) 58 entry["metadata"] = user.GetMetadata() 59 60 // User Roles 61 62 entry["roles"] = parsedUser.Claims.Roles 63 64 // Token 65 66 tokenMap := make(map[string]interface{}) 67 for k, v := range usr.AsMap() { 68 tokenMap[k] = v 69 } 70 tokenMap["authenticated"] = true 71 if usr.Claims.ExpiresAt > 0 { 72 tokenMap["expires_at_utc"] = time.Unix(usr.Claims.ExpiresAt, 0).Format(time.UnixDate) 73 } 74 if usr.Claims.IssuedAt > 0 { 75 tokenMap["issued_at_utc"] = time.Unix(usr.Claims.IssuedAt, 0).Format(time.UnixDate) 76 } 77 if usr.Claims.NotBefore > 0 { 78 tokenMap["not_before_utc"] = time.Unix(usr.Claims.NotBefore, 0).Format(time.UnixDate) 79 } 80 prettyTokenMap, err := json.MarshalIndent(tokenMap, "", " ") 81 if err != nil { 82 p.logger.Debug( 83 "failed to prettify user token", 84 zap.String("session_id", rr.Upstream.SessionID), 85 zap.String("request_id", rr.ID), 86 zap.Error(err), 87 ) 88 return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp) 89 } 90 entry["token"] = string(prettyTokenMap) 91 92 // Finalize 93 94 resp["entry"] = entry 95 return handleAPIProfileResponse(w, rr, http.StatusOK, resp) 96 }