Skip to content

Commit

Permalink
fix: refactor authorizer v2 (SSHcom#162)
Browse files Browse the repository at this point in the history
* fix: refactor authorizer v2

* feat(authorizer): support handling of custom param structs with url tags

* revert(auth/authorizer): rename ID

* fix(response): rename struct field to ID

* fix(authorizer): remove not requried handlers, fix naming in structs
  • Loading branch information
iljaSL committed Nov 28, 2024
1 parent 92f84ff commit 819912a
Show file tree
Hide file tree
Showing 9 changed files with 1,108 additions and 528 deletions.
52 changes: 27 additions & 25 deletions api/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package auth

import (
"net/url"

"github.com/SSHcom/privx-sdk-go/api/filters"
"github.com/SSHcom/privx-sdk-go/api/response"
"github.com/SSHcom/privx-sdk-go/restapi"
Expand Down Expand Up @@ -47,75 +49,75 @@ func (c *Auth) CreateIdpClient(idpClient *IdpClient) (response.Identifier, error
}

// UpdateIdpClient updates existing identity provider client configuration definition.
func (c *Auth) UpdateIdpClient(idpClient *IdpClient, idpId string) error {
func (c *Auth) UpdateIdpClient(idpClient *IdpClient, idpID string) error {
_, err := c.api.
URL("/auth/api/v1/idp/clients/%s", idpId).
URL("/auth/api/v1/idp/clients/%s", idpID).
Put(&idpClient)

return err
}

// GetIdpClient get existing identity provider client configuration.
func (c *Auth) GetIdpClient(idpId string) (*IdpClient, error) {
func (c *Auth) GetIdpClient(idpID string) (*IdpClient, error) {
idpClient := &IdpClient{}

_, err := c.api.
URL("/auth/api/v1/idp/clients/%s", idpId).
URL("/auth/api/v1/idp/clients/%s", idpID).
Get(&idpClient)

return idpClient, err
}

// DeleteIdpClient delete identity provider client configuration by Id.
func (c *Auth) DeleteIdpClient(idpId string) error {
// DeleteIdpClient delete identity provider client configuration by id.
func (c *Auth) DeleteIdpClient(idpID string) error {
_, err := c.api.
URL("/auth/api/v1/idp/clients/%s", idpId).
URL("/auth/api/v1/idp/clients/%s", idpID).
Delete()

return err
}

// RegenerateIdpClientConfig regenerates client_id and client_secret
// for OIDC identity provider client configuration.
func (c *Auth) RegenerateIdpClientConfig(idpId string) (*IdpClientConfig, error) {
func (c *Auth) RegenerateIdpClientConfig(idpID string) (*IdpClientConfig, error) {
clientConfig := &IdpClientConfig{}

_, err := c.api.
URL("/auth/api/v1/idp/clients/%s/regenerate", idpId).
URL("/auth/api/v1/idp/clients/%s/regenerate", idpID).
Post(nil, &clientConfig)

return clientConfig, err
}

// MARK: Session Storage
// GetUserSessions get valid sessions by userID.
func (c *Auth) GetUserSessions(userId string, opts ...filters.Option) (*response.ResultSet[Session], error) {
func (c *Auth) GetUserSessions(userID string, opts ...filters.Option) (*response.ResultSet[Session], error) {
userSessions := &response.ResultSet[Session]{}
params := filters.Default()
params := url.Values{}

for _, opt := range opts {
opt(&params)
}

_, err := c.api.
URL("/auth/api/v1/sessionstorage/users/%s/sessions", userId).
URL("/auth/api/v1/sessionstorage/users/%s/sessions", userID).
Query(params).
Get(&userSessions)

return userSessions, err
}

// GetSourceSessions get valid sessions by sourceID.
func (c *Auth) GetSourceSessions(sourceId string, opts ...filters.Option) (*response.ResultSet[Session], error) {
func (c *Auth) GetSourceSessions(sourceID string, opts ...filters.Option) (*response.ResultSet[Session], error) {
sourceSessions := &response.ResultSet[Session]{}
params := filters.Default()
params := url.Values{}

for _, opt := range opts {
opt(&params)
}

_, err := c.api.
URL("/auth/api/v1/sessionstorage/sources/%s/sessions", sourceId).
URL("/auth/api/v1/sessionstorage/sources/%s/sessions", sourceID).
Query(params).
Get(&sourceSessions)

Expand All @@ -125,7 +127,7 @@ func (c *Auth) GetSourceSessions(sourceId string, opts ...filters.Option) (*resp
// SearchSessions searches for sessions
func (c *Auth) SearchSessions(search *SessionSearch, opts ...filters.Option) (*response.ResultSet[Session], error) {
sessions := &response.ResultSet[Session]{}
params := filters.Default()
params := url.Values{}

for _, opt := range opts {
opt(&params)
Expand All @@ -139,19 +141,19 @@ func (c *Auth) SearchSessions(search *SessionSearch, opts ...filters.Option) (*r
return sessions, err
}

// TerminateSession terminates single session by Id.
func (c *Auth) TerminateSession(sessionId string) error {
// TerminateSession terminates single session by id.
func (c *Auth) TerminateSession(sessionID string) error {
_, err := c.api.
URL("/auth/api/v1/sessionstorage/sessions/%s/terminate", sessionId).
URL("/auth/api/v1/sessionstorage/sessions/%s/terminate", sessionID).
Post(nil)

return err
}

// TerminateUserSessions terminates all sessions for a user.
func (store *Auth) TerminateUserSessions(userId string) error {
func (store *Auth) TerminateUserSessions(userID string) error {
_, err := store.api.
URL("/auth/api/v1/sessionstorage/users/%s/sessions/terminate", userId).
URL("/auth/api/v1/sessionstorage/users/%s/sessions/terminate", userID).
Post(nil)

return err
Expand All @@ -169,20 +171,20 @@ func (store *Auth) Logout() error {

// MARK: Mobile Gateway
// GetUserPairedDevices get users paired devices.
func (store *Auth) GetUserPairedDevices(userId string) (*response.ResultSet[Device], error) {
func (store *Auth) GetUserPairedDevices(userID string) (*response.ResultSet[Device], error) {
devices := &response.ResultSet[Device]{}

_, err := store.api.
URL("/auth/api/v1/users/%s/devices", userId).
URL("/auth/api/v1/users/%s/devices", userID).
Get(devices)

return devices, err
}

// UnpairUserDevice unpair users device.
func (store *Auth) UnpairUserDevice(userId, deviceId string) error {
func (store *Auth) UnpairUserDevice(userID, deviceID string) error {
_, err := store.api.
URL("/auth/api/v1/users/%s/devices/%s", userId, deviceId).
URL("/auth/api/v1/users/%s/devices/%s", userID, deviceID).
Delete()

return err
Expand Down
19 changes: 10 additions & 9 deletions api/auth/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import "time"

// IdpClient identity provider client definition.
type IdpClient struct {
Id string `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Created time.Time `json:"created,omitempty"`
Updated time.Time `json:"updated,omitempty"`
IdpType string `json:"idp_type"`
IDPType string `json:"idp_type"`
OIDCIssuer string `json:"oidc_issuer,omitempty"`
OIDCAudience []string `json:"oidc_audience"`
OIDCClientId string `json:"oidc_client_id,omitempty"`
OIDCClientID string `json:"oidc_client_id,omitempty"`
OIDCClientSecret string `json:"oidc_client_secret,omitempty"`
OIDCScopesEnabled []string `json:"oidc_scopes_enabled"`
OIDCResponseTypesSupported []string `json:"oidc_response_types_supported,omitempty"`
Expand All @@ -35,6 +35,7 @@ type IdpClient struct {
OIDCRefreshTokenValidInMinutes int `json:"oidc_refresh_token_valid_in_minutes,omitempty"`
UserFilter string `json:"user_filter,omitempty"`
Enabled bool `json:"enabled"`
ContainerRequired bool `json:"container_required,omitempty"`
}

// IdpClientConfig identity provider client config definition.
Expand All @@ -45,15 +46,15 @@ type IdpClientConfig struct {

// Session session definition
type Session struct {
Id string `json:"id"`
UserId string `json:"user_id"`
SourceId string `json:"source_id"`
ID string `json:"id"`
UserID string `json:"user_id"`
SourceID string `json:"source_id"`
Domain string `json:"domain"`
Username string `json:"username"`
RemoteAddr string `json:"remote_addr"`
UserAgent string `json:"user_agent"`
Type string `json:"type"`
ParentSessionId string `json:"parent_session_id,omitempty"`
ParentSessionID string `json:"parent_session_id,omitempty"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Expires time.Time `json:"expires"`
Expand All @@ -77,13 +78,13 @@ type SessionPasswordPolicy struct {
// SessionSearch session search request parameter definition.
type SessionSearch struct {
Keywords string `json:"keywords,omitempty"`
UserId string `json:"user_id,omitempty"`
UserID string `json:"user_id,omitempty"`
Type string `json:"type,omitempty"`
}

// Device paired mobile gateway device definition.
type Device struct {
Id string `json:"id"`
ID string `json:"id"`
OS string `json:"os"`
Name string `json:"name"`
Activated string `json:"activated"`
Expand Down
Loading

0 comments on commit 819912a

Please sign in to comment.