Files
zt/controller/model/enrollment_mod_updb.go
Andrew MartinezandGitHub 6502098179 Fix.api.session.synth.jwt (#2935)
* fix #2934 fix API Session Cert to OIDC JWT mapping verification
* fix #2931 log invalid token aud, label token vs ext jwt aud
2025-03-26 11:24:17 -04:00

119 lines
3.1 KiB
Go

/*
Copyright NetFoundry Inc.
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
https://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 model
import (
"encoding/base64"
"errors"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/ziti/common/cert"
"github.com/openziti/ziti/common/eid"
"github.com/openziti/ziti/controller/apierror"
"github.com/openziti/ziti/controller/change"
"github.com/openziti/ziti/controller/db"
"github.com/openziti/ziti/controller/models"
"time"
)
type EnrollModuleUpdb struct {
env Env
method string
fingerprintGenerator cert.FingerprintGenerator
}
func NewEnrollModuleUpdb(env Env) *EnrollModuleUpdb {
return &EnrollModuleUpdb{
env: env,
method: db.MethodEnrollUpdb,
fingerprintGenerator: cert.NewFingerprintGenerator(),
}
}
func (module *EnrollModuleUpdb) CanHandle(method string) bool {
return method == module.method
}
func (module *EnrollModuleUpdb) Process(ctx EnrollmentContext) (*EnrollmentResult, error) {
enrollment, err := module.env.GetManagers().Enrollment.ReadByToken(ctx.GetToken())
if err != nil {
return nil, err
}
if enrollment == nil || enrollment.IdentityId == nil {
return nil, apierror.NewInvalidEnrollmentToken()
}
if enrollment.ExpiresAt == nil || enrollment.ExpiresAt.IsZero() || enrollment.ExpiresAt.Before(time.Now()) {
return nil, apierror.NewEnrollmentExpired()
}
identity, err := module.env.GetManagers().Identity.Read(*enrollment.IdentityId)
if err != nil {
return nil, err
}
if identity == nil {
return nil, apierror.NewInvalidEnrollmentToken()
}
ctx.GetChangeContext().
SetChangeAuthorType(change.AuthorTypeIdentity).
SetChangeAuthorId(identity.Id).
SetChangeAuthorName(identity.Name)
data := ctx.GetData()
if len(data.Password) == 0 {
return nil, errorz.NewUnhandled(errors.New("password expected for updb enrollment"))
}
hash := Hash(data.Password)
encodedPassword := base64.StdEncoding.EncodeToString(hash.Hash)
encodedSalt := base64.StdEncoding.EncodeToString(hash.Salt)
newAuthenticator := &Authenticator{
BaseEntity: models.BaseEntity{
Id: eid.New(),
},
Method: db.MethodAuthenticatorUpdb,
IdentityId: *enrollment.IdentityId,
SubType: &AuthenticatorUpdb{
Username: *enrollment.Username,
Password: encodedPassword,
Salt: encodedSalt,
},
}
err = module.env.GetManagers().Enrollment.ReplaceWithAuthenticator(enrollment.Id, newAuthenticator, ctx.GetChangeContext())
if err != nil {
return nil, err
}
return &EnrollmentResult{
Identity: identity,
Authenticator: newAuthenticator,
Content: map[string]interface{}{
"username": *enrollment.Username,
},
Status: 200,
}, nil
}