-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add certificate based authentication for the provider (#352)
* adding cert based auth * Update index.md.tmpl for typo --------- Co-authored-by: Matt Dotson <[email protected]>
- Loading branch information
Showing
9 changed files
with
316 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package powerplatform_helpers | ||
|
||
import ( | ||
"crypto" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
pkcs12 "software.sslmate.com/src/go-pkcs12" | ||
) | ||
|
||
func GetCertificateRawFromCertOrFilePath(certificate, certificateFilePath string) (string, error) { | ||
if certificate != "" { | ||
return strings.TrimSpace(certificate), nil | ||
} | ||
if certificateFilePath != "" { | ||
pfx, err := os.ReadFile(certificateFilePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
certAsBase64 := base64.StdEncoding.EncodeToString(pfx) | ||
return strings.TrimSpace(certAsBase64), nil | ||
} | ||
return "", errors.New("either client_certificate base64 or certificate_file_path must be provided") | ||
} | ||
|
||
func ConvertBase64ToCert(b64, password string) ([]*x509.Certificate, crypto.PrivateKey, error) { | ||
pfx, err := convertBase64ToByte(b64) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
certs, key, err := convertByteToCert(pfx, password) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return certs, key, nil | ||
} | ||
|
||
func convertBase64ToByte(b64 string) ([]byte, error) { | ||
if b64 == "" { | ||
return nil, errors.New("got empty base64 certificate data") | ||
} | ||
|
||
pfx, err := base64.StdEncoding.DecodeString(b64) | ||
if err != nil { | ||
return pfx, fmt.Errorf("could not decode base64 certificate data: %w", err) | ||
} | ||
|
||
return pfx, nil | ||
} | ||
|
||
func convertByteToCert(certData []byte, password string) ([]*x509.Certificate, crypto.PrivateKey, error) { | ||
var key crypto.PrivateKey | ||
|
||
key, cert, _, err := pkcs12.DecodeChain(certData, password) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if cert == nil { | ||
return nil, nil, errors.New("found no certificate") | ||
} | ||
|
||
certs := []*x509.Certificate{cert} | ||
|
||
return certs, key, nil | ||
} |
Oops, something went wrong.