Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
heiytor committed Dec 2, 2024
1 parent c7317ef commit b73c9ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 47 deletions.
2 changes: 0 additions & 2 deletions api/services/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func (s *service) SystemGetInfo(ctx context.Context, req requests.SystemGetInfo)
system.Endpoints.API = req.Host
}

system.SAML.GetAuthURL()

return system, nil
}

Expand Down
9 changes: 9 additions & 0 deletions gateway/nginx/conf.d/shellhub.conf
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ server {
proxy_pass http://upstream_router;
}

location /api/saml/acs {
{{ set_upstream "cloud-api" 8080 }}

auth_request off;
proxy_set_header X-Real-IP $x_real_ip;
proxy_set_header X-Forwarded-Host $host;
proxy_pass http://upstream_router;
}

location /api/register {
{{ set_upstream "cloud-api" 8080 }}

Expand Down
77 changes: 32 additions & 45 deletions pkg/models/system.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,48 @@
package models

import (
"crypto/x509"
"encoding/base64"

saml2 "github.com/russellhaering/gosaml2"
dsig "github.com/russellhaering/goxmldsig"
)

type System struct {
Version string `json:"version"`
Endpoints *SystemEndpoints `json:"endpoints"`
Setup bool `json:"setup" bson:"setup"`
AllowManualSignin bool `json:"allow_manual_signin" bson:"allow_manual_signin"`
SAML *SystemSAML `json:"sso" bson:"sso"`
Version string `json:"version"`
Endpoints *SystemEndpoints `json:"endpoints"`
Setup bool `json:"setup" bson:"setup"`
// Authentication manages the settings for available authentication methods, such as manual
// username/password authentication and SAML authentication. Each authentication method
// can be individually enabled or disabled.
Authentication *SystemAuthentication `json:"authentication" bson:"authentication"`
}

type SystemEndpoints struct {
API string `json:"api"`
SSH string `json:"ssh"`
}

type SystemSAML struct {
Enabled bool `json:"enabled" bson:"enabled"`
AuthURL string `json:"auth_url" bson:"-"`
IdpEntityID string `json:"-" bson:"idp_entity_id"`
IdpSignonLocation string `json:"-" bson:"idp_signon_location"`
IdpLogoutLocation string `json:"-" bson:"idp_logout_location"`
IdpCertificate string `json:"-" bson:"idp_certificate"`
type SystemAuthentication struct {
// Manual indicates whether manual authentication using a username and password is enabled or
// not.
Manual bool `json:"manual" bson:"manual"`
// SAML contains the configuration settings for SAML authentication. [SAML.Enabled] indicates
// whether SAML authentication is enabled or not.
SAML *SystemSAML `json:"saml" bson:"saml"`
}

func (s *SystemSAML) decodeCertificate() *dsig.MemoryX509CertificateStore {
certData, err := base64.StdEncoding.DecodeString(s.IdpCertificate)
if err != nil {
panic(err)
}

idpCert, err := x509.ParseCertificate(certData)
if err != nil {
panic(err)
}

certStore := new(dsig.MemoryX509CertificateStore)
certStore.Roots = append(certStore.Roots, idpCert)

return certStore
type SystemSAML struct {
// Enabled indicates whether SAML authentication is enabled.
Enabled bool `json:"enabled" bson:"enabled"`
Idp *SystemIdpSAML `json:"-" bson:"idp"`
Sp *SystemSpSAML `json:"-" bson:"sp"`
}

func (s *SystemSAML) GetAuthURL() {
sp := &saml2.SAMLServiceProvider{
IdentityProviderSSOURL: s.IdpSignonLocation,
IdentityProviderIssuer: s.IdpEntityID,
AssertionConsumerServiceURL: "http://localhost:3334/v1/_saml_callback",
SignAuthnRequests: true,
IDPCertificateStore: s.decodeCertificate(),
SPKeyStore: dsig.RandomKeyStoreForTest(),
}
type SystemIdpSAML struct {
EntityID string `json:"-" bson:"entity_id"`
SignonURL string `json:"-" bson:"signon_url"`
// Certificate is the IdP's X.509 certificate used to validate the authenticity of SAML assertions.
Certificate string `json:"-" bson:"certificate"`
}

s.AuthURL, _ = sp.BuildAuthURL("")
type SystemSpSAML struct {
// Certificate is the SP X.509 certificate used to enable mutual verification
// between the SP and IdP. The IdP uses this certificate to validate that authentication requests
// are signed by the SP, it also disable the "IdP-initiated" login flows.
//
// If this field is empty, the mutual verification behavior is disabled.
Certificate string `json:"-" bson:"certificate"`
}

0 comments on commit b73c9ae

Please sign in to comment.