Skip to content

Commit

Permalink
feature: add keys to profile api
Browse files Browse the repository at this point in the history
  • Loading branch information
greenpau committed Mar 19, 2024
1 parent f2c2b5d commit c9e5dc0
Show file tree
Hide file tree
Showing 17 changed files with 578 additions and 27 deletions.
54 changes: 54 additions & 0 deletions pkg/authn/api_delete_user_api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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/user"
)

// DeleteUserAPIKey deletes API key from user identity.
func (p *Portal) DeleteUserAPIKey(
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 {

rr.Key.Usage = "api"
if v, exists := bodyData["id"]; exists {
rr.Key.ID = v.(string)
} else {
resp["message"] = "Profile API did not find id in the request payload"
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
}

if err := backend.Request(operator.DeleteAPIKey, rr); err != nil {
resp["message"] = "Profile API failed to delete user api key"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
}

resp["entry"] = rr.Key.ID
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}
54 changes: 54 additions & 0 deletions pkg/authn/api_delete_user_gpg_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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/user"
)

// DeleteUserGPGKey deletes GPG key from user identity.
func (p *Portal) DeleteUserGPGKey(
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 {

rr.Key.Usage = "gpg"
if v, exists := bodyData["id"]; exists {
rr.Key.ID = v.(string)
} else {
resp["message"] = "Profile API did not find id in the request payload"
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
}

if err := backend.Request(operator.DeletePublicKey, rr); err != nil {
resp["message"] = "Profile API failed to delete user GPG key"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
}

resp["entry"] = rr.Key.ID
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}
1 change: 0 additions & 1 deletion pkg/authn/api_delete_user_multi_factor_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (p *Portal) DeleteUserMultiFactorVerifier(
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
}

// Get MFA Token
if err := backend.Request(operator.DeleteMfaToken, rr); err != nil {
resp["message"] = "Profile API failed to delete user multi factor authenticator"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
Expand Down
54 changes: 54 additions & 0 deletions pkg/authn/api_delete_user_ssh_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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/user"
)

// DeleteUserSSHKey deletes SSH key from user identity.
func (p *Portal) DeleteUserSSHKey(
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 {

rr.Key.Usage = "ssh"
if v, exists := bodyData["id"]; exists {
rr.Key.ID = v.(string)
} else {
resp["message"] = "Profile API did not find id in the request payload"
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
}

if err := backend.Request(operator.DeletePublicKey, rr); err != nil {
resp["message"] = "Profile API failed to delete user SSH key"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
}

resp["entry"] = rr.Key.ID
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}
48 changes: 48 additions & 0 deletions pkg/authn/api_fetch_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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/ids"
"github.com/greenpau/go-authcrunch/pkg/requests"
"github.com/greenpau/go-authcrunch/pkg/user"
)

// FetchDebug fetches debug information.
func (p *Portal) FetchDebug(
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 {

entry := make(map[string]interface{})
database := map[string]interface{}{
"name": "localdb",
"host": "localhost",
"port": 5432,
"engine": "postgresql",
}
entry["version"] = "1.0.0"
entry["database"] = database
resp["entry"] = entry
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}
76 changes: 76 additions & 0 deletions pkg/authn/api_fetch_user_api_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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/identity"
"github.com/greenpau/go-authcrunch/pkg/ids"
"github.com/greenpau/go-authcrunch/pkg/requests"
"github.com/greenpau/go-authcrunch/pkg/user"
)

// FetchUserAPIKeys fetches API keys from user identity.
func (p *Portal) FetchUserAPIKeys(
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 {

rr.Key.Usage = "api"
if err := backend.Request(operator.GetAPIKeys, rr); err != nil {
resp["message"] = "Profile API failed to get API keys"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
}
bundle := rr.Response.Payload.(*identity.APIKeyBundle)
resp["entries"] = bundle.Get()
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}

// FetchUserAPIKey fetches API key from user identity.
func (p *Portal) FetchUserAPIKey(
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 {

rr.Key.Usage = "api"
if v, exists := bodyData["id"]; exists {
rr.Key.ID = v.(string)
} else {
resp["message"] = "Profile API did not find id in the request payload"
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
}

if err := backend.Request(operator.GetAPIKey, rr); err != nil {
resp["message"] = "Profile API failed to get API key"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
}
token := rr.Response.Payload.(*identity.APIKey)
resp["entry"] = token
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}
76 changes: 76 additions & 0 deletions pkg/authn/api_fetch_user_gpg_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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/identity"
"github.com/greenpau/go-authcrunch/pkg/ids"
"github.com/greenpau/go-authcrunch/pkg/requests"
"github.com/greenpau/go-authcrunch/pkg/user"
)

// FetchUserGPGKeys fetches GPG keys from user identity.
func (p *Portal) FetchUserGPGKeys(
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 {

rr.Key.Usage = "gpg"
if err := backend.Request(operator.GetPublicKeys, rr); err != nil {
resp["message"] = "Profile API failed to get GPG keys"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
}
bundle := rr.Response.Payload.(*identity.PublicKeyBundle)
resp["entries"] = bundle.Get()
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}

// FetchUserGPGKey fetches GPG key from user identity.
func (p *Portal) FetchUserGPGKey(
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 {

rr.Key.Usage = "gpg"
if v, exists := bodyData["id"]; exists {
rr.Key.ID = v.(string)
} else {
resp["message"] = "Profile API did not find id in the request payload"
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp)
}

if err := backend.Request(operator.GetPublicKey, rr); err != nil {
resp["message"] = "Profile API failed to get GPG key"
return handleAPIProfileResponse(w, rr, http.StatusInternalServerError, resp)
}
token := rr.Response.Payload.(*identity.PublicKey)
resp["entry"] = token
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}
2 changes: 1 addition & 1 deletion pkg/authn/api_fetch_user_multi_factor_authenticators.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (p *Portal) FetchUserMultiFactorVerifiers(
return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
}

// FetchUserMultiFactorVerifier fetches app multi factor authenticators from user identity.
// FetchUserMultiFactorVerifier fetches app multi factor authenticator from user identity.
func (p *Portal) FetchUserMultiFactorVerifier(
ctx context.Context,
w http.ResponseWriter,
Expand Down
Loading

0 comments on commit c9e5dc0

Please sign in to comment.