mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package scram_test
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/hanzoai/mail/scram"
|
|
)
|
|
|
|
func Example() {
|
|
check := func(err error, msg string) {
|
|
if err != nil {
|
|
log.Fatalf("%s: %s", msg, err)
|
|
}
|
|
}
|
|
|
|
// Prepare credentials.
|
|
//
|
|
// The client normally remembers the password and uses it during authentication.
|
|
//
|
|
// The server sets the iteration count, generates a salt and uses the password once
|
|
// to generate salted password hash. The salted password hash is used to
|
|
// authenticate the client during authentication.
|
|
iterations := 4096
|
|
salt := scram.MakeRandom()
|
|
password := "test1234"
|
|
saltedPassword, err := scram.SaltPassword(sha256.New, password, salt, iterations)
|
|
check(err, "salt password")
|
|
|
|
// Make a new client for authenticating user mjl with SCRAM-SHA-256.
|
|
username := "mjl"
|
|
authz := ""
|
|
client := scram.NewClient(sha256.New, username, authz, false, nil)
|
|
clientFirst, err := client.ClientFirst()
|
|
check(err, "client.ClientFirst")
|
|
|
|
// Instantiate a new server with the initial message from the client.
|
|
server, err := scram.NewServer(sha256.New, []byte(clientFirst), nil, false)
|
|
check(err, "NewServer")
|
|
|
|
// Generate first message from server to client, with a challenge.
|
|
serverFirst, err := server.ServerFirst(iterations, salt)
|
|
check(err, "server.ServerFirst")
|
|
|
|
// Continue at client with first message from server, resulting in message from
|
|
// client to server.
|
|
clientFinal, err := client.ServerFirst([]byte(serverFirst), password)
|
|
check(err, "client.ServerFirst")
|
|
|
|
// Continue at server with message from client.
|
|
// The server authenticates the client in this step.
|
|
serverFinal, err := server.Finish([]byte(clientFinal), saltedPassword)
|
|
if err != nil {
|
|
fmt.Println("server does not accept client credentials")
|
|
} else {
|
|
fmt.Println("server has accepted client credentials")
|
|
}
|
|
|
|
// Finally, the client verifies that the server knows the salted password hash.
|
|
err = client.ServerFinal([]byte(serverFinal))
|
|
if err != nil {
|
|
fmt.Println("client does not accept server")
|
|
} else {
|
|
fmt.Println("client has accepted server")
|
|
}
|
|
|
|
// Output:
|
|
// server has accepted client credentials
|
|
// client has accepted server
|
|
}
|