From 3c5c3ea7fb8e464eae81cf601611520f4a0b4eab Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 14 Feb 2022 14:03:30 +0100 Subject: [PATCH] Add methods to query the policy version --- userapi/api/api.go | 22 ++++++++++++++++++++++ userapi/api/api_trace.go | 12 ++++++++++++ userapi/internal/api.go | 28 ++++++++++++++++++++++++++++ userapi/inthttp/client.go | 34 ++++++++++++++++++++++++++-------- userapi/inthttp/server.go | 28 ++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 8 deletions(-) diff --git a/userapi/api/api.go b/userapi/api/api.go index 46a13d971..e5a173cec 100644 --- a/userapi/api/api.go +++ b/userapi/api/api.go @@ -44,6 +44,8 @@ type UserInternalAPI interface { QueryDeviceInfos(ctx context.Context, req *QueryDeviceInfosRequest, res *QueryDeviceInfosResponse) error QuerySearchProfiles(ctx context.Context, req *QuerySearchProfilesRequest, res *QuerySearchProfilesResponse) error QueryOpenIDToken(ctx context.Context, req *QueryOpenIDTokenRequest, res *QueryOpenIDTokenResponse) error + QueryPolicyVersion(ctx context.Context, req *QueryPolicyVersionRequest, res *QueryPolicyVersionResponse) error + GetOutdatedPolicy(ctx context.Context, req *QueryOutdatedPolicyUsersRequest, res *QueryOutdatedPolicyUsersResponse) error } type PerformKeyBackupRequest struct { @@ -335,6 +337,26 @@ type QueryOpenIDTokenResponse struct { ExpiresAtMS int64 } +// QueryPolicyVersionRequest is the response for QueryPolicyVersionRequest +type QueryPolicyVersionRequest struct { + LocalPart string +} + +// QueryPolicyVersionResponsestruct is the response for QueryPolicyVersionResponsestruct +type QueryPolicyVersionResponse struct { + PolicyVersion string +} + +// QueryOutdatedPolicyUsersRequest is the response for QueryOutdatedPolicyUsersRequest +type QueryOutdatedPolicyUsersRequest struct { + PolicyVersion string +} + +// QueryOutdatedPolicyUsersResponse is the response for QueryOutdatedPolicyUsersRequest +type QueryOutdatedPolicyUsersResponse struct { + OutdatedUsers []string +} + // Device represents a client's device (mobile, web, etc) type Device struct { ID string diff --git a/userapi/api/api_trace.go b/userapi/api/api_trace.go index aa069f40b..15b5572f4 100644 --- a/userapi/api/api_trace.go +++ b/userapi/api/api_trace.go @@ -119,6 +119,18 @@ func (t *UserInternalAPITrace) QueryOpenIDToken(ctx context.Context, req *QueryO return err } +func (t *UserInternalAPITrace) QueryPolicyVersion(ctx context.Context, req *QueryPolicyVersionRequest, res *QueryPolicyVersionResponse) error { + err := t.Impl.QueryPolicyVersion(ctx, req, res) + util.GetLogger(ctx).Infof("QueryPolicyVersion req=%+v res=%+v", js(req), js(res)) + return err +} + +func (t *UserInternalAPITrace) GetOutdatedPolicy(ctx context.Context, req *QueryOutdatedPolicyUsersRequest, res *QueryOutdatedPolicyUsersResponse) error { + err := t.Impl.GetOutdatedPolicy(ctx, req, res) + util.GetLogger(ctx).Infof("QueryPolicyVersion req=%+v res=%+v", js(req), js(res)) + return err +} + func js(thing interface{}) string { b, err := json.Marshal(thing) if err != nil { diff --git a/userapi/internal/api.go b/userapi/internal/api.go index 5d91383de..0d7a723a9 100644 --- a/userapi/internal/api.go +++ b/userapi/internal/api.go @@ -587,3 +587,31 @@ func (a *UserInternalAPI) QueryKeyBackup(ctx context.Context, req *api.QueryKeyB } res.Keys = result } + +func (a *UserInternalAPI) QueryPolicyVersion( + ctx context.Context, + req *api.QueryPolicyVersionRequest, + res *api.QueryPolicyVersionResponse, +) error { + var err error + res.PolicyVersion, err = a.AccountDB.GetPrivacyPolicy(ctx, req.LocalPart) + if err != nil { + return err + } + + return nil +} + +func (a *UserInternalAPI) GetOutdatedPolicy( + ctx context.Context, + req *api.QueryOutdatedPolicyUsersRequest, + res *api.QueryOutdatedPolicyUsersResponse, +) error { + var err error + res.OutdatedUsers, err = a.AccountDB.GetOutdatedPolicy(ctx, req.PolicyVersion) + if err != nil { + return err + } + + return nil +} diff --git a/userapi/inthttp/client.go b/userapi/inthttp/client.go index 1599d4639..4a99d1b13 100644 --- a/userapi/inthttp/client.go +++ b/userapi/inthttp/client.go @@ -38,14 +38,16 @@ const ( PerformOpenIDTokenCreationPath = "/userapi/performOpenIDTokenCreation" PerformKeyBackupPath = "/userapi/performKeyBackup" - QueryKeyBackupPath = "/userapi/queryKeyBackup" - QueryProfilePath = "/userapi/queryProfile" - QueryAccessTokenPath = "/userapi/queryAccessToken" - QueryDevicesPath = "/userapi/queryDevices" - QueryAccountDataPath = "/userapi/queryAccountData" - QueryDeviceInfosPath = "/userapi/queryDeviceInfos" - QuerySearchProfilesPath = "/userapi/querySearchProfiles" - QueryOpenIDTokenPath = "/userapi/queryOpenIDToken" + QueryKeyBackupPath = "/userapi/queryKeyBackup" + QueryProfilePath = "/userapi/queryProfile" + QueryAccessTokenPath = "/userapi/queryAccessToken" + QueryDevicesPath = "/userapi/queryDevices" + QueryAccountDataPath = "/userapi/queryAccountData" + QueryDeviceInfosPath = "/userapi/queryDeviceInfos" + QuerySearchProfilesPath = "/userapi/querySearchProfiles" + QueryOpenIDTokenPath = "/userapi/queryOpenIDToken" + QueryPolicyVersion = "/userapi/queryPolicyVersion" + QueryOutdatedPolicyUsers = "/userapi/queryOutdatedPolicy" ) // NewUserAPIClient creates a UserInternalAPI implemented by talking to a HTTP POST API. @@ -249,3 +251,19 @@ func (h *httpUserInternalAPI) QueryKeyBackup(ctx context.Context, req *api.Query res.Error = err.Error() } } + +func (h *httpUserInternalAPI) QueryPolicyVersion(ctx context.Context, req *api.QueryPolicyVersionRequest, res *api.QueryPolicyVersionResponse) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryKeyBackup") + defer span.Finish() + + apiURL := h.apiURL + QueryPolicyVersion + return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res) +} + +func (h *httpUserInternalAPI) GetOutdatedPolicy(ctx context.Context, req *api.QueryOutdatedPolicyUsersRequest, res *api.QueryOutdatedPolicyUsersResponse) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryKeyBackup") + defer span.Finish() + + apiURL := h.apiURL + QueryOutdatedPolicyUsers + return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res) +} diff --git a/userapi/inthttp/server.go b/userapi/inthttp/server.go index d00ee042c..22e49181c 100644 --- a/userapi/inthttp/server.go +++ b/userapi/inthttp/server.go @@ -265,4 +265,32 @@ func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) { return util.JSONResponse{Code: http.StatusOK, JSON: &response} }), ) + internalAPIMux.Handle(QueryPolicyVersion, + httputil.MakeInternalAPI("queryPolicyVersion", func(req *http.Request) util.JSONResponse { + request := api.QueryPolicyVersionRequest{} + response := api.QueryPolicyVersionResponse{} + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + err := s.QueryPolicyVersion(req.Context(), &request, &response) + if err != nil { + return util.JSONResponse{Code: http.StatusBadRequest, JSON: &response} + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(QueryOutdatedPolicyUsers, + httputil.MakeInternalAPI("queryOutdatedPolicyUsers", func(req *http.Request) util.JSONResponse { + request := api.QueryOutdatedPolicyUsersRequest{} + response := api.QueryOutdatedPolicyUsersResponse{} + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + err := s.GetOutdatedPolicy(req.Context(), &request, &response) + if err != nil { + return util.JSONResponse{Code: http.StatusBadRequest, JSON: &response} + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) }