Update README.md
This commit is contained in:
parent
769b6ff4d9
commit
d3002cc8b4
39
README.md
39
README.md
@ -45,32 +45,31 @@ The way this is implemented in this package is like so:
|
|||||||
|
|
||||||
## TOFU
|
## TOFU
|
||||||
|
|
||||||
This package provides an easy way to implement Trust On First Use in your
|
`go-gemini` makes it easy to implement Trust On First Use in your clients.
|
||||||
clients. Here is a simple client using TOFU to authenticate certificates:
|
|
||||||
|
Clients can load the default list of known hosts:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
client := &gemini.Client{
|
client := &Client{}
|
||||||
KnownHosts: gemini.LoadKnownHosts(),
|
knownHosts, err := gemini.LoadKnownHosts()
|
||||||
TrustCertificate: func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
client.KnownHosts = knownHosts
|
||||||
|
```
|
||||||
|
|
||||||
|
Clients can then specify how to trust certificates in the `TrustCertificate`
|
||||||
|
field:
|
||||||
|
|
||||||
|
```go
|
||||||
|
client.TrustCertificate = func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
||||||
// If the certificate is in the known hosts list, allow the connection
|
// If the certificate is in the known hosts list, allow the connection
|
||||||
if err := knownHosts.Lookup(cert); {
|
return knownHosts.Lookup(cert)
|
||||||
return true
|
|
||||||
}
|
|
||||||
// Prompt the user
|
|
||||||
if userTrustsCertificateTemporarily() {
|
|
||||||
// Temporarily trust the certificate
|
|
||||||
return true
|
|
||||||
} else if userTrustsCertificatePermanently() {
|
|
||||||
// Add the certificate to the known hosts file
|
|
||||||
knownHosts.Add(cert)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// User does not trust the certificate
|
|
||||||
return false
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Advanced clients can prompt the user for what to do when encountering an unknown certificate:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
client := &gemini.Client{
|
client := &gemini.Client{
|
||||||
TrustCertificate: func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
TrustCertificate: func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
client = &gemini.Client{
|
client = &gemini.Client{
|
||||||
|
KnownHosts: gemini.LoadKnownHosts(),
|
||||||
TrustCertificate: func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
TrustCertificate: func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
||||||
// Trust all certificates
|
// Trust all certificates
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user