EdgexAgent/device-ble-go/vendor/github.com/openziti/identity/certtools/requests.go
2025-07-10 20:40:32 +08:00

59 lines
1.6 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 certtools
import (
"crypto/x509"
"crypto/x509/pkix"
)
var EMPTY = []string{}
type CsrConfig struct {
AltNames []string `json:"altNames"`
Country string `json:"country"`
Locality string `json:"locality"`
Organization string `json:"organization"`
OrganizationalUnit string `json:"organizationalUnit"`
Province string `json:"province"`
}
func NewCertRequest(subject map[string]string, dns []string) (*x509.CertificateRequest, error) {
request := &x509.CertificateRequest{
Subject: pkix.Name{
Country: getField(subject["C"]),
Province: getField(subject["S"]),
Locality: getField(subject["L"]),
Organization: getField(subject["O"]),
OrganizationalUnit: getField(subject["OU"]),
CommonName: subject["CN"],
},
DNSNames: dns,
ExtraExtensions: []pkix.Extension{},
}
return request, nil
}
func getField(f string) []string {
if f != "" {
return []string{f}
}
return EMPTY
}