diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e10e5d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# ignore special macOS files +.DS_Store + +# ignore directories +binaries/ +assets/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..b9d253e --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +# discourse-user-api-key + +### Purpose +With a User-API-Key it is possible to perform certain actions automatically in a (Discourse) forum. The User-API-Key is bound to a user. The actions are performed on behalf of the user for whom the User-API-Key was created. + +This utility allows to request a User-API-Key from a (Discourse) forum. The utility is available as command line interface application for different operating systems. + +### Binaries +Precompiled binaries for many operating systems can be found here: **Releases -> Assets** + +### Usage +``` text +% ./discourse-user-api-key -h + +Program: + Name : discourse-user-api-key + Release : v1.0.0 - 2022/10/31 + Purpose : obtain Discourse User-API-Key + Info : This program obtains a User-API-Key for a Discourse forum. + +Usage: + ./discourse-user-api-key -forum=string [-application=string] [-client=string] [-scopes=list] [-nonce=string] [-verbose] + +Examples: + ./discourse-user-api-key -forum=community.openstreetmap.org + ./discourse-user-api-key -forum=meta.discourse.org -application=UltimateReaderWriter -scopes=read,write + +Options: + -application string + name of application shown on forum site (default "GenericDiscourseReader") + -client string + client ID (default [generated unique UUID4]) + -forum string + Discourse forum URL + -nonce string + random string generated once (default [generated URL-safe random string]) + -scopes string + comma-separated list of access scopes allowed for the key (default "read") + -verbose + verbose output (maybe helpful in case of problems) + +Workflow for getting an User-API-Key: + Step 1: copy forum URL into your browser + Step 2: authorize application access on forum site + Step 3: copy encrypted User-API-Key data from forum site in here + Step 4: save User-API-Key into your key vault +``` + +### Example +``` text +% ./discourse-user-api-key -forum=community.openstreetmap.org + +Program: + Name : discourse-user-api-key + Release : v1.0.0 - 2022/10/31 + Purpose : obtain Discourse User-API-Key + Info : This program obtains a User-API-Key for a Discourse forum. + +Workflow for getting an User-API-Key: + Step 1: copy forum URL into your browser + Step 2: authorize application access on forum site + Step 3: copy encrypted User-API-Key data from forum site in here + Step 4: save User-API-Key into your key vault + +Step 1: copy forum URL into your browser ... + +https://community.openstreetmap.org/user-api-key/new?application_name=GenericDiscourseReader&client_id=47d3c48e-b6b4-4f5b-8822-6203aaa9ed97&nonce=3J9rujWeEQP_IEwasM-r6HN-sqY%3D&public_key=-----BEGIN+PUBLIC+KEY-----%0AMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo26hAwBt8dnQ93nwpFBw%0AX0KoxuskYvt7n6RN5l6NUKWsCTdC%2BGf%2Ftu2VvgTBPfgxDIfun2uIvF6n743m10qB%0AieCm8VIdPK2Yc3h4h58KfkZ4G9s%2BbJeLLv3g7uDCNMG8A2%2F8u5XSCxN%2FeuIkstvr%0AAmTBClrInJrq6pUELpgyoyZbNl1DpUl%2BX92%2FfUeGgdORHiKorAbZT4PCC5HE72pG%0AAdv45i6EvCHUfuISkB56DK4CduGH0v5dzZTXlnFi6iaNpH0pd8rJTvRCmrV9VutG%0A96fv%2B1MutjwfWaL90WJ2JwF%2FEVWfBe4cQATCVqRN5lQ79tdY5UwgPuyXS%2Big9xuB%0ALwIDAQAB%0A-----END+PUBLIC+KEY-----%0A&scopes=read + +Step 2: authorize application access on forum site ... + +Step 3: copy encrypted User-API-Key data from forum site in here (and press Enter) ... + +Pp7IxXlZ4Yb6nCnQfJ5htMaffA9TAaH9RztOUYvI2IssBxZXWOQ5+LOJRtqO sTWJYAUFrIwwYPhJGq5Dc4oA7VFxy8cOVDfq3VkDNw5tA1T3OGeysiIjyRC8 KbzDZdS8xYhFdIyI9rYzhbnYeCazY/33LG/4UFf+Ld1nkx0GtwLGi8D75+sK 6ECnw/oDwGMoUFLjlQ4p47mOa0bDGh7xUS2h3VO7xee13ctvj873dqRmiPuN xaTsQXtf3v9EwVAY1Hj+xqrex78yj8U3hmyEXqssoGhNn8QlVAmn8E93dcdu TeSf9fhhgSoePc1a/kiEdRTlGVMcR5+WDqfNg1wDPw== + +Decrypted User-API-Key data = {"key":"aa141fc6a7362231178d8d8e5609e768","nonce":"3J9rujWeEQP_IEwasM-r6HN-sqY=","push":false,"api":4} + +User-API-Key = aa141fc6a7362231178d8d8e5609e768 +----------------------------------------------- + +Step 4: save User-API-Key into your key vault +``` diff --git a/build-assets.sh b/build-assets.sh new file mode 100644 index 0000000..a3c46e2 --- /dev/null +++ b/build-assets.sh @@ -0,0 +1,60 @@ +#!/bin/sh + +# ------------------------------------ +# Purpose: +# - Builds assets (tar.gz or zip). +# +# Releases: +# - v1.0.0 - 2022-10-31: initial release +# ------------------------------------ + +# set -o xtrace +set -o verbose + +# recreate directory +rm -r ./assets +mkdir ./assets + +# asset 'aix' +tar -cvzf ./assets/aix-ppc64_discourse-user-api-key.tar.gz ./binaries/aix-ppc64/discourse-user-api-key + +# assets 'darwin' +tar -cvzf ./assets/darwin-amd64_discourse-user-api-key.tar.gz ./binaries/darwin-amd64/discourse-user-api-key +tar -cvzf ./assets/darwin-arm64_discourse-user-api-key.tar.gz ./binaries/darwin-arm64/discourse-user-api-key + +# assets 'dragonfly' +tar -cvzf ./assets/dragonfly-amd64_discourse-user-api-key.tar.gz ./binaries/dragonfly-amd64/discourse-user-api-key + +# assets 'freebsd' +tar -cvzf ./assets/freebsd-amd64_discourse-user-api-key.tar.gz/freebsd-amd64/discourse-user-api-key +tar -cvzf ./assets/freebsd-arm64_discourse-user-api-key.tar.gz ./binaries/freebsd-arm64/discourse-user-api-key + +# asset 'illumos' +tar -cvzf ./assets/illumos-amd64_discourse-user-api-key.tar.gz ./binaries/illumos-amd64/discourse-user-api-key + +# assets 'linux' +tar -cvzf ./assets/linux-amd64_discourse-user-api-key.tar.gz ./binaries/linux-amd64/discourse-user-api-key +tar -cvzf ./assets/linux-arm64_discourse-user-api-key.tar.gz ./binaries/linux-arm64/discourse-user-api-key +tar -cvzf ./assets/linux-mips64_discourse-user-api-key.tar.gz ./binaries/linux-mips64/discourse-user-api-key +tar -cvzf ./assets/linux-mips64le_discourse-user-api-key.tar.gz ./binaries/linux-mips64le/discourse-user-api-key +tar -cvzf ./assets/linux-ppc64_discourse-user-api-key.tar.gz ./binaries/linux-ppc64/discourse-user-api-key +tar -cvzf ./assets/linux-ppc64le_discourse-user-api-key.tar.gz ./binaries/linux-ppc64le/discourse-user-api-key +tar -cvzf ./assets/linux-riscv64_discourse-user-api-key.tar.gz ./binaries/linux-riscv64/discourse-user-api-key +tar -cvzf ./assets/linux-s390x_discourse-user-api-key.tar.gz ./binaries/linux-s390x/discourse-user-api-key + +# assets 'netbsd' +tar -cvzf ./assets/netbsd-amd64_discourse-user-api-key.tar.gz ./binaries/netbsd-amd64/discourse-user-api-key +tar -cvzf ./assets/netbsd-arm64_discourse-user-api-key.tar.gz ./binaries/netbsd-arm64/discourse-user-api-key + +# assets 'openbsd' +tar -cvzf ./assets/openbsd-amd64_discourse-user-api-key.tar.gz ./binaries/openbsd-amd64/discourse-user-api-key +tar -cvzf ./assets/openbsd-arm64_discourse-user-api-key.tar.gz ./binaries/openbsd-arm64/discourse-user-api-key +tar -cvzf ./assets/openbsd-mips64_discourse-user-api-key.tar.gz ./binaries/openbsd-mips64/discourse-user-api-key + +# asset 'solaris' +tar -cvzf ./assets/solaris-amd64_discourse-user-api-key.tar.gz ./binaries/solaris-amd64/discourse-user-api-key + +# assets 'windows' +zip ./assets/windows-amd64_discourse-user-api-key.zip ./binaries/windows-amd64/discourse-user-api-key.exe +zip ./assets/windows-386_discourse-user-api-key.zip ./binaries/windows-386/discourse-user-api-key.exe +zip ./assets/windows-arm_discourse-user-api-key.zip ./binaries/windows-arm/discourse-user-api-key.exe diff --git a/build-binaries.sh b/build-binaries.sh new file mode 100644 index 0000000..f1dabc9 --- /dev/null +++ b/build-binaries.sh @@ -0,0 +1,62 @@ +#!/bin/sh + +# ------------------------------------ +# Purpose: +# - Builds executables / binaries. +# +# Releases: +# - v1.0.0 - 2022-10-31: initial release +# +# Remarks: +# - go tool dist list +# ------------------------------------ + +# set -o xtrace +set -o verbose + +# renew vendor content +go mod vendor + +# compile 'aix' +env GOOS=aix GOARCH=ppc64 go build -o binaries/aix-ppc64/discourse-user-api-key + +# compile 'darwin' +env GOOS=darwin GOARCH=amd64 go build -o binaries/darwin-amd64/discourse-user-api-key +env GOOS=darwin GOARCH=arm64 go build -o binaries/darwin-arm64/discourse-user-api-key + +# compile 'dragonfly' +env GOOS=dragonfly GOARCH=amd64 go build -o binaries/dragonfly-amd64/discourse-user-api-key + +# compile 'freebsd' +env GOOS=freebsd GOARCH=amd64 go build -o binaries/freebsd-amd64/discourse-user-api-key +env GOOS=freebsd GOARCH=arm64 go build -o binaries/freebsd-arm64/discourse-user-api-key + +# compile 'illumos' +env GOOS=illumos GOARCH=amd64 go build -o binaries/illumos-amd64/discourse-user-api-key + +# compile 'linux' +env GOOS=linux GOARCH=amd64 go build -o binaries/linux-amd64/discourse-user-api-key +env GOOS=linux GOARCH=arm64 go build -o binaries/linux-arm64/discourse-user-api-key +env GOOS=linux GOARCH=mips64 go build -o binaries/linux-mips64/discourse-user-api-key +env GOOS=linux GOARCH=mips64le go build -o binaries/linux-mips64le/discourse-user-api-key +env GOOS=linux GOARCH=ppc64 go build -o binaries/linux-ppc64/discourse-user-api-key +env GOOS=linux GOARCH=ppc64le go build -o binaries/linux-ppc64le/discourse-user-api-key +env GOOS=linux GOARCH=riscv64 go build -o binaries/linux-riscv64/discourse-user-api-key +env GOOS=linux GOARCH=s390x go build -o binaries/linux-s390x/discourse-user-api-key + +# compile 'netbsd' +env GOOS=netbsd GOARCH=amd64 go build -o binaries/netbsd-amd64/discourse-user-api-key +env GOOS=netbsd GOARCH=arm64 go build -o binaries/netbsd-arm64/discourse-user-api-key + +# compile 'openbsd' +env GOOS=openbsd GOARCH=amd64 go build -o binaries/openbsd-amd64/discourse-user-api-key +env GOOS=openbsd GOARCH=arm64 go build -o binaries/openbsd-arm64/discourse-user-api-key +env GOOS=openbsd GOARCH=mips64 go build -o binaries/openbsd-mips64/discourse-user-api-key + +# compile 'solaris' +env GOOS=solaris GOARCH=amd64 go build -o binaries/solaris-amd64/discourse-user-api-key + +# compile 'windows' +env GOOS=windows GOARCH=amd64 go build -o binaries/windows-amd64/discourse-user-api-key.exe +env GOOS=windows GOARCH=386 go build -o binaries/windows-386/discourse-user-api-key.exe +env GOOS=windows GOARCH=arm go build -o binaries/windows-arm/discourse-user-api-key.exe diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..afcaa91 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module klaus/discourse-user-api-key + +go 1.19 + +require github.com/gofrs/uuid v4.3.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0b2dcda --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gofrs/uuid v4.3.0+incompatible h1:CaSVZxm5B+7o45rtab4jC2G37WGYX1zQfuU2i6DSvnc= +github.com/gofrs/uuid v4.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..1802bf8 --- /dev/null +++ b/main.go @@ -0,0 +1,295 @@ +/* +Purpose: +- obtain Discourse User-API-Key + +Description: +- This program obtains a User-API-Key for a Discourse forum. + +Releases: +- v1.0.0 - 2022/10/31: initial release + +Author: +- Klaus Tockloth + +Copyright: +- Copyright (c) 2022 Klaus Tockloth + +Contact (eMail): +- freizeitkarte@googlemail.com + +License (MIT): +Permission is hereby granted, free of charge, to any person obtaining a copy of this software +and associated documentation files (the Software), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +The software is provided 'as is', without warranty of any kind, express or implied, including +but not limited to the warranties of merchantability, fitness for a particular purpose and +noninfringement. In no event shall the authors or copyright holders be liable for any claim, +damages or other liability, whether in an action of contract, tort or otherwise, arising from, +out of or in connection with the software or the use or other dealings in the software. + +Remarks: +- Lint: golangci-lint run --no-config --enable gocritic +- Vulnerability detection: govulncheck ./... + +Links: +- https://meta.discourse.org/t/user-api-keys-specification/48536 +- https://meta.discourse.org/t/generate-user-api-keys-for-testing/145744 +- https://stackoverflow.com/questions/13555085/save-and-load-crypto-rsa-privatekey-to-and-from-the-disk +- https://gist.github.com/dopey/c69559607800d2f2f90b1b1ed4e550fb +*/ + +package main + +import ( + "bufio" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "encoding/json" + "encoding/pem" + "flag" + "fmt" + "net/url" + "os" + "path/filepath" + "strings" + + "github.com/gofrs/uuid" +) + +// general program info +var ( + progName = filepath.Base(os.Args[0]) + progVersion = "v1.0.0" + progDate = "2022/10/31" + progPurpose = "obtain Discourse User-API-Key" + progInfo = "This program obtains a User-API-Key for a Discourse forum." +) + +// DiscourseUserApiKey represents User-API-Key structure returned from Discourse service +type DiscourseUserApiKey struct { + Key string `json:"key"` + Nonce string `json:"nonce"` + Push bool `json:"push"` + API int `json:"api"` +} + +/* +main starts this program. +*/ +func main() { + fmt.Printf("\nProgram:\n"+ + " Name : %s\n"+ + " Release : %s - %s\n"+ + " Purpose : %s\n"+ + " Info : %s\n", progName, progVersion, progDate, progPurpose, progInfo) + + forum := flag.String("forum", "", "Discourse forum URL") + application := flag.String("application", "GenericDiscourseReader", "name of application shown on forum site") + client := flag.String("client", "", "client ID (default [generated unique UUID4])") + scopes := flag.String("scopes", "read", "comma-separated list of access scopes allowed for the key") + nonce := flag.String("nonce", "", "random string generated once (default [generated URL-safe random string])") + verbose := flag.Bool("verbose", false, "verbose output (maybe helpful in case of problems)") + + flag.Usage = printUsage + flag.Parse() + + if *forum == "" { + fmt.Printf("\nError: mandatory option '-forum=string' missing\n") + printUsage() + } + + fmt.Printf("%s\n", workflow) + + if *verbose { + fmt.Printf("\nParameters from command line:\n"+ + " forum : %s\n"+ + " application : %s\n"+ + " client : %s\n"+ + " scopes : %s\n"+ + " nonce : %s\n", *forum, *application, *client, *scopes, *nonce) + } + + // generate RSA private key + DiscoursePrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + handleFatalError(fmt.Sprintf("error [%v] at rsa.GenerateKey()", err)) + } + if *verbose { + fmt.Printf("\nPrivate Key = %v", exportRSAPrivateKeyAsPEM(DiscoursePrivateKey)) + } + + // encode public key to PEM + DiscoursePublicKeyPEM, err := exportRSAPublicKeyAsPEM(&DiscoursePrivateKey.PublicKey) + if err != nil { + handleFatalError(fmt.Sprintf("error [%v] at exportRSAPublicKeyAsPEM()", err)) + } + if *verbose { + fmt.Printf("\nPublic Key = %v", DiscoursePublicKeyPEM) + } + + // create request parameters + if *client == "" { + uuidTemp, err := uuid.NewV4() + if err != nil { + handleFatalError(fmt.Sprintf("error [%v] at uuid.NewV4()", err)) + } + *client = uuidTemp.String() + } + if *nonce == "" { + randomStringTemp, err := generateRandomStringURLSafe(20) + if err != nil { + handleFatalError(fmt.Sprintf("error [%v] at generateRandomStringURLSafe()", err)) + } + *nonce = randomStringTemp + } + + if *verbose { + fmt.Printf("\nRequest parameters for URL:\n"+ + " forum : %s\n"+ + " application : %s\n"+ + " client : %s\n"+ + " scopes : %s\n"+ + " nonce : %s\n", *forum, *application, *client, *scopes, *nonce) + } + + // build URL + baseURL := fmt.Sprintf("https://%s/user-api-key/new", *forum) + values := url.Values{} + values.Set("application_name", *application) + values.Set("client_id", *client) + values.Set("scopes", *scopes) + values.Set("public_key", DiscoursePublicKeyPEM) + values.Set("nonce", *nonce) + + performURL := baseURL + "?" + values.Encode() + + fmt.Printf("\nStep 1: copy forum URL into your browser ...\n\n%s\n", performURL) + + fmt.Printf("\nStep 2: authorize application access on forum site ...\n") + + // read encrypted data from stdin (copied from browser) + fmt.Print("\nStep 3: copy encrypted User-API-Key data from forum site in here (and press Enter) ...\n\n") + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + + // verify, that something was copied in + if scanner.Text() == "" { + handleFatalError("User-API-Key data from forum site missing") + } + + // process encoded User-API-Key data from discourse service + encodedUserApiKey := strings.ReplaceAll(scanner.Text(), " ", "") + textUserApiKey, err := base64.StdEncoding.DecodeString(encodedUserApiKey) + if err != nil { + handleFatalError(fmt.Sprintf("error [%v] at base64.StdEncoding.DecodeString()", err)) + } + if *verbose { + fmt.Printf("\nEncrypted User-API-Key data = %s\n", encodedUserApiKey) + } + + decryptedUserApiKey, err := DiscoursePrivateKey.Decrypt(rand.Reader, textUserApiKey, nil) + if err != nil { + handleFatalError(fmt.Sprintf("error [%v] at DiscoursePrivateKey.Decrypt()", err)) + } + fmt.Printf("\nDecrypted User-API-Key data = %s\n", decryptedUserApiKey) + + var discourseUserApiKey DiscourseUserApiKey + err = json.Unmarshal(decryptedUserApiKey, &discourseUserApiKey) + if err != nil { + handleFatalError(fmt.Sprintf("error [%v] at json.Unmarshal()", err)) + } + + fmt.Printf("\nUser-API-Key = %s\n-----------------------------------------------\n", discourseUserApiKey.Key) + + fmt.Printf("\nStep 4: save User-API-Key into your key vault\n\n") +} + +/* +handleFatalError handles fatal error. +*/ +func handleFatalError(message string) { + fmt.Printf("Fatal Error: %s\n\n", message) + os.Exit(1) +} + +/* +exportRSAPrivateKeyAsPEM exports RSA private key as PEM-encoded string. +*/ +func exportRSAPrivateKeyAsPEM(privkey *rsa.PrivateKey) string { + privateKeyBytes := x509.MarshalPKCS1PrivateKey(privkey) + + privateKeyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privateKeyBytes}) + + return string(privateKeyPEM) +} + +/* +exportRSAPublicKeyAsPEM exports RSA public key as PEM-encoded string. +*/ +func exportRSAPublicKeyAsPEM(pubkey *rsa.PublicKey) (string, error) { + publicKeyBytes, err := x509.MarshalPKIXPublicKey(pubkey) + if err != nil { + return "", err + } + + publicKeyPEM := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: publicKeyBytes}) + + return string(publicKeyPEM), nil +} + +/* +generateRandomBytes returns securely generated random bytes. +*/ +func generateRandomBytes(n int) ([]byte, error) { + b := make([]byte, n) + _, err := rand.Read(b) + if err != nil { + return nil, err + } + + return b, nil +} + +/* +generateRandomStringURLSafe returns a URL-safe, base64 encoded securely generated random string. +*/ +func generateRandomStringURLSafe(n int) (string, error) { + b, err := generateRandomBytes(n) + + return base64.URLEncoding.EncodeToString(b), err +} + +/* +printUsage prints the usage of this program. +*/ +func printUsage() { + fmt.Printf("\nUsage:\n") + fmt.Printf(" %s -forum=string [-application=string] [-client=string] [-scopes=list] [-nonce=string] [-verbose]\n", os.Args[0]) + + fmt.Printf("\nExamples:\n") + fmt.Printf(" %s -forum=community.openstreetmap.org\n", os.Args[0]) + fmt.Printf(" %s -forum=meta.discourse.org -application=UltimateReaderWriter -scopes=read,write\n", os.Args[0]) + + fmt.Printf("\nOptions:\n") + flag.PrintDefaults() + + fmt.Printf("%s\n", workflow) + + fmt.Printf("\n") + os.Exit(1) +} + +var workflow = ` +Workflow for getting an User-API-Key: + Step 1: copy forum URL into your browser + Step 2: authorize application access on forum site + Step 3: copy encrypted User-API-Key data from forum site in here + Step 4: save User-API-Key into your key vault` diff --git a/vendor/github.com/gofrs/uuid/.gitignore b/vendor/github.com/gofrs/uuid/.gitignore new file mode 100644 index 0000000..666dbbb --- /dev/null +++ b/vendor/github.com/gofrs/uuid/.gitignore @@ -0,0 +1,15 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# binary bundle generated by go-fuzz +uuid-fuzz.zip diff --git a/vendor/github.com/gofrs/uuid/LICENSE b/vendor/github.com/gofrs/uuid/LICENSE new file mode 100644 index 0000000..926d549 --- /dev/null +++ b/vendor/github.com/gofrs/uuid/LICENSE @@ -0,0 +1,20 @@ +Copyright (C) 2013-2018 by Maxim Bublis + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/gofrs/uuid/README.md b/vendor/github.com/gofrs/uuid/README.md new file mode 100644 index 0000000..f5db14f --- /dev/null +++ b/vendor/github.com/gofrs/uuid/README.md @@ -0,0 +1,117 @@ +# UUID + +[![License](https://img.shields.io/github/license/gofrs/uuid.svg)](https://github.com/gofrs/uuid/blob/master/LICENSE) +[![Build Status](https://travis-ci.org/gofrs/uuid.svg?branch=master)](https://travis-ci.org/gofrs/uuid) +[![GoDoc](http://godoc.org/github.com/gofrs/uuid?status.svg)](http://godoc.org/github.com/gofrs/uuid) +[![Coverage Status](https://codecov.io/gh/gofrs/uuid/branch/master/graphs/badge.svg?branch=master)](https://codecov.io/gh/gofrs/uuid/) +[![Go Report Card](https://goreportcard.com/badge/github.com/gofrs/uuid)](https://goreportcard.com/report/github.com/gofrs/uuid) + +Package uuid provides a pure Go implementation of Universally Unique Identifiers +(UUID) variant as defined in RFC-4122. This package supports both the creation +and parsing of UUIDs in different formats. + +This package supports the following UUID versions: +* Version 1, based on timestamp and MAC address (RFC-4122) +* Version 3, based on MD5 hashing of a named value (RFC-4122) +* Version 4, based on random numbers (RFC-4122) +* Version 5, based on SHA-1 hashing of a named value (RFC-4122) + +This package also supports experimental Universally Unique Identifier implementations based on a +[draft RFC](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03) that updates RFC-4122 +* Version 6, a k-sortable id based on timestamp, and field-compatible with v1 (draft-peabody-dispatch-new-uuid-format, RFC-4122) +* Version 7, a k-sortable id based on timestamp (draft-peabody-dispatch-new-uuid-format, RFC-4122) + +The v6 and v7 IDs are **not** considered a part of the stable API, and may be subject to behavior or API changes as part of minor releases +to this package. They will be updated as the draft RFC changes, and will become stable if and when the draft RFC is accepted. + +## Project History + +This project was originally forked from the +[github.com/satori/go.uuid](https://github.com/satori/go.uuid) repository after +it appeared to be no longer maintained, while exhibiting [critical +flaws](https://github.com/satori/go.uuid/issues/73). We have decided to take +over this project to ensure it receives regular maintenance for the benefit of +the larger Go community. + +We'd like to thank Maxim Bublis for his hard work on the original iteration of +the package. + +## License + +This source code of this package is released under the MIT License. Please see +the [LICENSE](https://github.com/gofrs/uuid/blob/master/LICENSE) for the full +content of the license. + +## Recommended Package Version + +We recommend using v2.0.0+ of this package, as versions prior to 2.0.0 were +created before our fork of the original package and have some known +deficiencies. + +## Installation + +It is recommended to use a package manager like `dep` that understands tagged +releases of a package, as well as semantic versioning. + +If you are unable to make use of a dependency manager with your project, you can +use the `go get` command to download it directly: + +```Shell +$ go get github.com/gofrs/uuid +``` + +## Requirements + +Due to subtests not being supported in older versions of Go, this package is +only regularly tested against Go 1.7+. This package may work perfectly fine with +Go 1.2+, but support for these older versions is not actively maintained. + +## Go 1.11 Modules + +As of v3.2.0, this repository no longer adopts Go modules, and v3.2.0 no longer has a `go.mod` file. As a result, v3.2.0 also drops support for the `github.com/gofrs/uuid/v3` import path. Only module-based consumers are impacted. With the v3.2.0 release, _all_ gofrs/uuid consumers should use the `github.com/gofrs/uuid` import path. + +An existing module-based consumer will continue to be able to build using the `github.com/gofrs/uuid/v3` import path using any valid consumer `go.mod` that worked prior to the publishing of v3.2.0, but any module-based consumer should start using the `github.com/gofrs/uuid` import path when possible and _must_ use the `github.com/gofrs/uuid` import path prior to upgrading to v3.2.0. + +Please refer to [Issue #61](https://github.com/gofrs/uuid/issues/61) and [Issue #66](https://github.com/gofrs/uuid/issues/66) for more details. + +## Usage + +Here is a quick overview of how to use this package. For more detailed +documentation, please see the [GoDoc Page](http://godoc.org/github.com/gofrs/uuid). + +```go +package main + +import ( + "log" + + "github.com/gofrs/uuid" +) + +// Create a Version 4 UUID, panicking on error. +// Use this form to initialize package-level variables. +var u1 = uuid.Must(uuid.NewV4()) + +func main() { + // Create a Version 4 UUID. + u2, err := uuid.NewV4() + if err != nil { + log.Fatalf("failed to generate UUID: %v", err) + } + log.Printf("generated Version 4 UUID %v", u2) + + // Parse a UUID from a string. + s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8" + u3, err := uuid.FromString(s) + if err != nil { + log.Fatalf("failed to parse UUID %q: %v", s, err) + } + log.Printf("successfully parsed UUID %v", u3) +} +``` + +## References + +* [RFC-4122](https://tools.ietf.org/html/rfc4122) +* [DCE 1.1: Authentication and Security Services](http://pubs.opengroup.org/onlinepubs/9696989899/chap5.htm#tagcjh_08_02_01_01) +* [New UUID Formats RFC Draft (Peabody) Rev 03](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03) diff --git a/vendor/github.com/gofrs/uuid/codec.go b/vendor/github.com/gofrs/uuid/codec.go new file mode 100644 index 0000000..e3014c6 --- /dev/null +++ b/vendor/github.com/gofrs/uuid/codec.go @@ -0,0 +1,212 @@ +// Copyright (C) 2013-2018 by Maxim Bublis +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +package uuid + +import ( + "bytes" + "encoding/hex" + "fmt" +) + +// FromBytes returns a UUID generated from the raw byte slice input. +// It will return an error if the slice isn't 16 bytes long. +func FromBytes(input []byte) (UUID, error) { + u := UUID{} + err := u.UnmarshalBinary(input) + return u, err +} + +// FromBytesOrNil returns a UUID generated from the raw byte slice input. +// Same behavior as FromBytes(), but returns uuid.Nil instead of an error. +func FromBytesOrNil(input []byte) UUID { + uuid, err := FromBytes(input) + if err != nil { + return Nil + } + return uuid +} + +// FromString returns a UUID parsed from the input string. +// Input is expected in a form accepted by UnmarshalText. +func FromString(input string) (UUID, error) { + u := UUID{} + err := u.UnmarshalText([]byte(input)) + return u, err +} + +// FromStringOrNil returns a UUID parsed from the input string. +// Same behavior as FromString(), but returns uuid.Nil instead of an error. +func FromStringOrNil(input string) UUID { + uuid, err := FromString(input) + if err != nil { + return Nil + } + return uuid +} + +// MarshalText implements the encoding.TextMarshaler interface. +// The encoding is the same as returned by the String() method. +func (u UUID) MarshalText() ([]byte, error) { + return []byte(u.String()), nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface. +// Following formats are supported: +// +// "6ba7b810-9dad-11d1-80b4-00c04fd430c8", +// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", +// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" +// "6ba7b8109dad11d180b400c04fd430c8" +// "{6ba7b8109dad11d180b400c04fd430c8}", +// "urn:uuid:6ba7b8109dad11d180b400c04fd430c8" +// +// ABNF for supported UUID text representation follows: +// +// URN := 'urn' +// UUID-NID := 'uuid' +// +// hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | +// 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | +// 'A' | 'B' | 'C' | 'D' | 'E' | 'F' +// +// hexoct := hexdig hexdig +// 2hexoct := hexoct hexoct +// 4hexoct := 2hexoct 2hexoct +// 6hexoct := 4hexoct 2hexoct +// 12hexoct := 6hexoct 6hexoct +// +// hashlike := 12hexoct +// canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct +// +// plain := canonical | hashlike +// uuid := canonical | hashlike | braced | urn +// +// braced := '{' plain '}' | '{' hashlike '}' +// urn := URN ':' UUID-NID ':' plain +// +func (u *UUID) UnmarshalText(text []byte) error { + switch len(text) { + case 32: + return u.decodeHashLike(text) + case 34, 38: + return u.decodeBraced(text) + case 36: + return u.decodeCanonical(text) + case 41, 45: + return u.decodeURN(text) + default: + return fmt.Errorf("uuid: incorrect UUID length %d in string %q", len(text), text) + } +} + +// decodeCanonical decodes UUID strings that are formatted as defined in RFC-4122 (section 3): +// "6ba7b810-9dad-11d1-80b4-00c04fd430c8". +func (u *UUID) decodeCanonical(t []byte) error { + if t[8] != '-' || t[13] != '-' || t[18] != '-' || t[23] != '-' { + return fmt.Errorf("uuid: incorrect UUID format in string %q", t) + } + + src := t + dst := u[:] + + for i, byteGroup := range byteGroups { + if i > 0 { + src = src[1:] // skip dash + } + _, err := hex.Decode(dst[:byteGroup/2], src[:byteGroup]) + if err != nil { + return err + } + src = src[byteGroup:] + dst = dst[byteGroup/2:] + } + + return nil +} + +// decodeHashLike decodes UUID strings that are using the following format: +// "6ba7b8109dad11d180b400c04fd430c8". +func (u *UUID) decodeHashLike(t []byte) error { + src := t[:] + dst := u[:] + + _, err := hex.Decode(dst, src) + return err +} + +// decodeBraced decodes UUID strings that are using the following formats: +// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}" +// "{6ba7b8109dad11d180b400c04fd430c8}". +func (u *UUID) decodeBraced(t []byte) error { + l := len(t) + + if t[0] != '{' || t[l-1] != '}' { + return fmt.Errorf("uuid: incorrect UUID format in string %q", t) + } + + return u.decodePlain(t[1 : l-1]) +} + +// decodeURN decodes UUID strings that are using the following formats: +// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" +// "urn:uuid:6ba7b8109dad11d180b400c04fd430c8". +func (u *UUID) decodeURN(t []byte) error { + total := len(t) + + urnUUIDPrefix := t[:9] + + if !bytes.Equal(urnUUIDPrefix, urnPrefix) { + return fmt.Errorf("uuid: incorrect UUID format in string %q", t) + } + + return u.decodePlain(t[9:total]) +} + +// decodePlain decodes UUID strings that are using the following formats: +// "6ba7b810-9dad-11d1-80b4-00c04fd430c8" or in hash-like format +// "6ba7b8109dad11d180b400c04fd430c8". +func (u *UUID) decodePlain(t []byte) error { + switch len(t) { + case 32: + return u.decodeHashLike(t) + case 36: + return u.decodeCanonical(t) + default: + return fmt.Errorf("uuid: incorrect UUID length %d in string %q", len(t), t) + } +} + +// MarshalBinary implements the encoding.BinaryMarshaler interface. +func (u UUID) MarshalBinary() ([]byte, error) { + return u.Bytes(), nil +} + +// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. +// It will return an error if the slice isn't 16 bytes long. +func (u *UUID) UnmarshalBinary(data []byte) error { + if len(data) != Size { + return fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data)) + } + copy(u[:], data) + + return nil +} diff --git a/vendor/github.com/gofrs/uuid/fuzz.go b/vendor/github.com/gofrs/uuid/fuzz.go new file mode 100644 index 0000000..afaefbc --- /dev/null +++ b/vendor/github.com/gofrs/uuid/fuzz.go @@ -0,0 +1,47 @@ +// Copyright (c) 2018 Andrei Tudor Călin +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +// +build gofuzz + +package uuid + +// Fuzz implements a simple fuzz test for FromString / UnmarshalText. +// +// To run: +// +// $ go get github.com/dvyukov/go-fuzz/... +// $ cd $GOPATH/src/github.com/gofrs/uuid +// $ go-fuzz-build github.com/gofrs/uuid +// $ go-fuzz -bin=uuid-fuzz.zip -workdir=./testdata +// +// If you make significant changes to FromString / UnmarshalText and add +// new cases to fromStringTests (in codec_test.go), please run +// +// $ go test -seed_fuzz_corpus +// +// to seed the corpus with the new interesting inputs, then run the fuzzer. +func Fuzz(data []byte) int { + _, err := FromString(string(data)) + if err != nil { + return 0 + } + return 1 +} diff --git a/vendor/github.com/gofrs/uuid/generator.go b/vendor/github.com/gofrs/uuid/generator.go new file mode 100644 index 0000000..c790f70 --- /dev/null +++ b/vendor/github.com/gofrs/uuid/generator.go @@ -0,0 +1,356 @@ +// Copyright (C) 2013-2018 by Maxim Bublis +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +package uuid + +import ( + "crypto/md5" + "crypto/rand" + "crypto/sha1" + "encoding/binary" + "fmt" + "hash" + "io" + "net" + "sync" + "time" +) + +// Difference in 100-nanosecond intervals between +// UUID epoch (October 15, 1582) and Unix epoch (January 1, 1970). +const epochStart = 122192928000000000 + +type epochFunc func() time.Time + +// HWAddrFunc is the function type used to provide hardware (MAC) addresses. +type HWAddrFunc func() (net.HardwareAddr, error) + +// DefaultGenerator is the default UUID Generator used by this package. +var DefaultGenerator Generator = NewGen() + +// NewV1 returns a UUID based on the current timestamp and MAC address. +func NewV1() (UUID, error) { + return DefaultGenerator.NewV1() +} + +// NewV3 returns a UUID based on the MD5 hash of the namespace UUID and name. +func NewV3(ns UUID, name string) UUID { + return DefaultGenerator.NewV3(ns, name) +} + +// NewV4 returns a randomly generated UUID. +func NewV4() (UUID, error) { + return DefaultGenerator.NewV4() +} + +// NewV5 returns a UUID based on SHA-1 hash of the namespace UUID and name. +func NewV5(ns UUID, name string) UUID { + return DefaultGenerator.NewV5(ns, name) +} + +// NewV6 returns a k-sortable UUID based on a timestamp and 48 bits of +// pseudorandom data. The timestamp in a V6 UUID is the same as V1, with the bit +// order being adjusted to allow the UUID to be k-sortable. +// +// This is implemented based on revision 03 of the Peabody UUID draft, and may +// be subject to change pending further revisions. Until the final specification +// revision is finished, changes required to implement updates to the spec will +// not be considered a breaking change. They will happen as a minor version +// releases until the spec is final. +func NewV6() (UUID, error) { + return DefaultGenerator.NewV6() +} + +// NewV7 returns a k-sortable UUID based on the current millisecond precision +// UNIX epoch and 74 bits of pseudorandom data. +// +// This is implemented based on revision 03 of the Peabody UUID draft, and may +// be subject to change pending further revisions. Until the final specification +// revision is finished, changes required to implement updates to the spec will +// not be considered a breaking change. They will happen as a minor version +// releases until the spec is final. +func NewV7() (UUID, error) { + return DefaultGenerator.NewV7() +} + +// Generator provides an interface for generating UUIDs. +type Generator interface { + NewV1() (UUID, error) + NewV3(ns UUID, name string) UUID + NewV4() (UUID, error) + NewV5(ns UUID, name string) UUID + NewV6() (UUID, error) + NewV7() (UUID, error) +} + +// Gen is a reference UUID generator based on the specifications laid out in +// RFC-4122 and DCE 1.1: Authentication and Security Services. This type +// satisfies the Generator interface as defined in this package. +// +// For consumers who are generating V1 UUIDs, but don't want to expose the MAC +// address of the node generating the UUIDs, the NewGenWithHWAF() function has been +// provided as a convenience. See the function's documentation for more info. +// +// The authors of this package do not feel that the majority of users will need +// to obfuscate their MAC address, and so we recommend using NewGen() to create +// a new generator. +type Gen struct { + clockSequenceOnce sync.Once + hardwareAddrOnce sync.Once + storageMutex sync.Mutex + + rand io.Reader + + epochFunc epochFunc + hwAddrFunc HWAddrFunc + lastTime uint64 + clockSequence uint16 + hardwareAddr [6]byte +} + +// interface check -- build will fail if *Gen doesn't satisfy Generator +var _ Generator = (*Gen)(nil) + +// NewGen returns a new instance of Gen with some default values set. Most +// people should use this. +func NewGen() *Gen { + return NewGenWithHWAF(defaultHWAddrFunc) +} + +// NewGenWithHWAF builds a new UUID generator with the HWAddrFunc provided. Most +// consumers should use NewGen() instead. +// +// This is used so that consumers can generate their own MAC addresses, for use +// in the generated UUIDs, if there is some concern about exposing the physical +// address of the machine generating the UUID. +// +// The Gen generator will only invoke the HWAddrFunc once, and cache that MAC +// address for all the future UUIDs generated by it. If you'd like to switch the +// MAC address being used, you'll need to create a new generator using this +// function. +func NewGenWithHWAF(hwaf HWAddrFunc) *Gen { + return &Gen{ + epochFunc: time.Now, + hwAddrFunc: hwaf, + rand: rand.Reader, + } +} + +// NewV1 returns a UUID based on the current timestamp and MAC address. +func (g *Gen) NewV1() (UUID, error) { + u := UUID{} + + timeNow, clockSeq, err := g.getClockSequence() + if err != nil { + return Nil, err + } + binary.BigEndian.PutUint32(u[0:], uint32(timeNow)) + binary.BigEndian.PutUint16(u[4:], uint16(timeNow>>32)) + binary.BigEndian.PutUint16(u[6:], uint16(timeNow>>48)) + binary.BigEndian.PutUint16(u[8:], clockSeq) + + hardwareAddr, err := g.getHardwareAddr() + if err != nil { + return Nil, err + } + copy(u[10:], hardwareAddr) + + u.SetVersion(V1) + u.SetVariant(VariantRFC4122) + + return u, nil +} + +// NewV3 returns a UUID based on the MD5 hash of the namespace UUID and name. +func (g *Gen) NewV3(ns UUID, name string) UUID { + u := newFromHash(md5.New(), ns, name) + u.SetVersion(V3) + u.SetVariant(VariantRFC4122) + + return u +} + +// NewV4 returns a randomly generated UUID. +func (g *Gen) NewV4() (UUID, error) { + u := UUID{} + if _, err := io.ReadFull(g.rand, u[:]); err != nil { + return Nil, err + } + u.SetVersion(V4) + u.SetVariant(VariantRFC4122) + + return u, nil +} + +// NewV5 returns a UUID based on SHA-1 hash of the namespace UUID and name. +func (g *Gen) NewV5(ns UUID, name string) UUID { + u := newFromHash(sha1.New(), ns, name) + u.SetVersion(V5) + u.SetVariant(VariantRFC4122) + + return u +} + +// NewV6 returns a k-sortable UUID based on a timestamp and 48 bits of +// pseudorandom data. The timestamp in a V6 UUID is the same as V1, with the bit +// order being adjusted to allow the UUID to be k-sortable. +// +// This is implemented based on revision 03 of the Peabody UUID draft, and may +// be subject to change pending further revisions. Until the final specification +// revision is finished, changes required to implement updates to the spec will +// not be considered a breaking change. They will happen as a minor version +// releases until the spec is final. +func (g *Gen) NewV6() (UUID, error) { + var u UUID + + if _, err := io.ReadFull(g.rand, u[10:]); err != nil { + return Nil, err + } + + timeNow, clockSeq, err := g.getClockSequence() + if err != nil { + return Nil, err + } + + binary.BigEndian.PutUint32(u[0:], uint32(timeNow>>28)) // set time_high + binary.BigEndian.PutUint16(u[4:], uint16(timeNow>>12)) // set time_mid + binary.BigEndian.PutUint16(u[6:], uint16(timeNow&0xfff)) // set time_low (minus four version bits) + binary.BigEndian.PutUint16(u[8:], clockSeq&0x3fff) // set clk_seq_hi_res (minus two variant bits) + + u.SetVersion(V6) + u.SetVariant(VariantRFC4122) + + return u, nil +} + +// getClockSequence returns the epoch and clock sequence for V1 and V6 UUIDs. +func (g *Gen) getClockSequence() (uint64, uint16, error) { + var err error + g.clockSequenceOnce.Do(func() { + buf := make([]byte, 2) + if _, err = io.ReadFull(g.rand, buf); err != nil { + return + } + g.clockSequence = binary.BigEndian.Uint16(buf) + }) + if err != nil { + return 0, 0, err + } + + g.storageMutex.Lock() + defer g.storageMutex.Unlock() + + timeNow := g.getEpoch() + // Clock didn't change since last UUID generation. + // Should increase clock sequence. + if timeNow <= g.lastTime { + g.clockSequence++ + } + g.lastTime = timeNow + + return timeNow, g.clockSequence, nil +} + +// NewV7 returns a k-sortable UUID based on the current millisecond precision +// UNIX epoch and 74 bits of pseudorandom data. +// +// This is implemented based on revision 03 of the Peabody UUID draft, and may +// be subject to change pending further revisions. Until the final specification +// revision is finished, changes required to implement updates to the spec will +// not be considered a breaking change. They will happen as a minor version +// releases until the spec is final. +func (g *Gen) NewV7() (UUID, error) { + var u UUID + + if _, err := io.ReadFull(g.rand, u[6:]); err != nil { + return Nil, err + } + + tn := g.epochFunc() + ms := uint64(tn.UnixMilli()) + u[0] = byte(ms >> 40) + u[1] = byte(ms >> 32) + u[2] = byte(ms >> 24) + u[3] = byte(ms >> 16) + u[4] = byte(ms >> 8) + u[5] = byte(ms) + + u.SetVersion(V7) + u.SetVariant(VariantRFC4122) + + return u, nil +} + +// Returns the hardware address. +func (g *Gen) getHardwareAddr() ([]byte, error) { + var err error + g.hardwareAddrOnce.Do(func() { + var hwAddr net.HardwareAddr + if hwAddr, err = g.hwAddrFunc(); err == nil { + copy(g.hardwareAddr[:], hwAddr) + return + } + + // Initialize hardwareAddr randomly in case + // of real network interfaces absence. + if _, err = io.ReadFull(g.rand, g.hardwareAddr[:]); err != nil { + return + } + // Set multicast bit as recommended by RFC-4122 + g.hardwareAddr[0] |= 0x01 + }) + if err != nil { + return []byte{}, err + } + return g.hardwareAddr[:], nil +} + +// Returns the difference between UUID epoch (October 15, 1582) +// and current time in 100-nanosecond intervals. +func (g *Gen) getEpoch() uint64 { + return epochStart + uint64(g.epochFunc().UnixNano()/100) +} + +// Returns the UUID based on the hashing of the namespace UUID and name. +func newFromHash(h hash.Hash, ns UUID, name string) UUID { + u := UUID{} + h.Write(ns[:]) + h.Write([]byte(name)) + copy(u[:], h.Sum(nil)) + + return u +} + +var netInterfaces = net.Interfaces + +// Returns the hardware address. +func defaultHWAddrFunc() (net.HardwareAddr, error) { + ifaces, err := netInterfaces() + if err != nil { + return []byte{}, err + } + for _, iface := range ifaces { + if len(iface.HardwareAddr) >= 6 { + return iface.HardwareAddr, nil + } + } + return []byte{}, fmt.Errorf("uuid: no HW address found") +} diff --git a/vendor/github.com/gofrs/uuid/sql.go b/vendor/github.com/gofrs/uuid/sql.go new file mode 100644 index 0000000..6f254a4 --- /dev/null +++ b/vendor/github.com/gofrs/uuid/sql.go @@ -0,0 +1,109 @@ +// Copyright (C) 2013-2018 by Maxim Bublis +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +package uuid + +import ( + "bytes" + "database/sql/driver" + "encoding/json" + "fmt" +) + +// Value implements the driver.Valuer interface. +func (u UUID) Value() (driver.Value, error) { + return u.String(), nil +} + +// Scan implements the sql.Scanner interface. +// A 16-byte slice will be handled by UnmarshalBinary, while +// a longer byte slice or a string will be handled by UnmarshalText. +func (u *UUID) Scan(src interface{}) error { + switch src := src.(type) { + case UUID: // support gorm convert from UUID to NullUUID + *u = src + return nil + + case []byte: + if len(src) == Size { + return u.UnmarshalBinary(src) + } + return u.UnmarshalText(src) + + case string: + return u.UnmarshalText([]byte(src)) + } + + return fmt.Errorf("uuid: cannot convert %T to UUID", src) +} + +// NullUUID can be used with the standard sql package to represent a +// UUID value that can be NULL in the database. +type NullUUID struct { + UUID UUID + Valid bool +} + +// Value implements the driver.Valuer interface. +func (u NullUUID) Value() (driver.Value, error) { + if !u.Valid { + return nil, nil + } + // Delegate to UUID Value function + return u.UUID.Value() +} + +// Scan implements the sql.Scanner interface. +func (u *NullUUID) Scan(src interface{}) error { + if src == nil { + u.UUID, u.Valid = Nil, false + return nil + } + + // Delegate to UUID Scan function + u.Valid = true + return u.UUID.Scan(src) +} + +// MarshalJSON marshals the NullUUID as null or the nested UUID +func (u NullUUID) MarshalJSON() ([]byte, error) { + if !u.Valid { + return json.Marshal(nil) + } + + return json.Marshal(u.UUID) +} + +// UnmarshalJSON unmarshals a NullUUID +func (u *NullUUID) UnmarshalJSON(b []byte) error { + if bytes.Equal(b, []byte("null")) { + u.UUID, u.Valid = Nil, false + return nil + } + + if err := json.Unmarshal(b, &u.UUID); err != nil { + return err + } + + u.Valid = true + + return nil +} diff --git a/vendor/github.com/gofrs/uuid/uuid.go b/vendor/github.com/gofrs/uuid/uuid.go new file mode 100644 index 0000000..e747e54 --- /dev/null +++ b/vendor/github.com/gofrs/uuid/uuid.go @@ -0,0 +1,292 @@ +// Copyright (C) 2013-2018 by Maxim Bublis +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +// Package uuid provides implementations of the Universally Unique Identifier +// (UUID), as specified in RFC-4122 and the Peabody RFC Draft (revision 03). +// +// RFC-4122[1] provides the specification for versions 1, 3, 4, and 5. The +// Peabody UUID RFC Draft[2] provides the specification for the new k-sortable +// UUIDs, versions 6 and 7. +// +// DCE 1.1[3] provides the specification for version 2, but version 2 support +// was removed from this package in v4 due to some concerns with the +// specification itself. Reading the spec, it seems that it would result in +// generating UUIDs that aren't very unique. In having read the spec it seemed +// that our implementation did not meet the spec. It also seems to be at-odds +// with RFC 4122, meaning we would need quite a bit of special code to support +// it. Lastly, there were no Version 2 implementations that we could find to +// ensure we were understanding the specification correctly. +// +// [1] https://tools.ietf.org/html/rfc4122 +// [2] https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03 +// [3] http://pubs.opengroup.org/onlinepubs/9696989899/chap5.htm#tagcjh_08_02_01_01 +package uuid + +import ( + "encoding/binary" + "encoding/hex" + "fmt" + "io" + "strings" + "time" +) + +// Size of a UUID in bytes. +const Size = 16 + +// UUID is an array type to represent the value of a UUID, as defined in RFC-4122. +type UUID [Size]byte + +// UUID versions. +const ( + _ byte = iota + V1 // Version 1 (date-time and MAC address) + _ // Version 2 (date-time and MAC address, DCE security version) [removed] + V3 // Version 3 (namespace name-based) + V4 // Version 4 (random) + V5 // Version 5 (namespace name-based) + V6 // Version 6 (k-sortable timestamp and random data, field-compatible with v1) [peabody draft] + V7 // Version 7 (k-sortable timestamp and random data) [peabody draft] + _ // Version 8 (k-sortable timestamp, meant for custom implementations) [peabody draft] [not implemented] +) + +// UUID layout variants. +const ( + VariantNCS byte = iota + VariantRFC4122 + VariantMicrosoft + VariantFuture +) + +// UUID DCE domains. +const ( + DomainPerson = iota + DomainGroup + DomainOrg +) + +// Timestamp is the count of 100-nanosecond intervals since 00:00:00.00, +// 15 October 1582 within a V1 UUID. This type has no meaning for other +// UUID versions since they don't have an embedded timestamp. +type Timestamp uint64 + +const _100nsPerSecond = 10000000 + +// Time returns the UTC time.Time representation of a Timestamp +func (t Timestamp) Time() (time.Time, error) { + secs := uint64(t) / _100nsPerSecond + nsecs := 100 * (uint64(t) % _100nsPerSecond) + + return time.Unix(int64(secs)-(epochStart/_100nsPerSecond), int64(nsecs)), nil +} + +// TimestampFromV1 returns the Timestamp embedded within a V1 UUID. +// Returns an error if the UUID is any version other than 1. +func TimestampFromV1(u UUID) (Timestamp, error) { + if u.Version() != 1 { + err := fmt.Errorf("uuid: %s is version %d, not version 1", u, u.Version()) + return 0, err + } + + low := binary.BigEndian.Uint32(u[0:4]) + mid := binary.BigEndian.Uint16(u[4:6]) + hi := binary.BigEndian.Uint16(u[6:8]) & 0xfff + + return Timestamp(uint64(low) + (uint64(mid) << 32) + (uint64(hi) << 48)), nil +} + +// TimestampFromV6 returns the Timestamp embedded within a V6 UUID. This +// function returns an error if the UUID is any version other than 6. +// +// This is implemented based on revision 03 of the Peabody UUID draft, and may +// be subject to change pending further revisions. Until the final specification +// revision is finished, changes required to implement updates to the spec will +// not be considered a breaking change. They will happen as a minor version +// releases until the spec is final. +func TimestampFromV6(u UUID) (Timestamp, error) { + if u.Version() != 6 { + return 0, fmt.Errorf("uuid: %s is version %d, not version 6", u, u.Version()) + } + + hi := binary.BigEndian.Uint32(u[0:4]) + mid := binary.BigEndian.Uint16(u[4:6]) + low := binary.BigEndian.Uint16(u[6:8]) & 0xfff + + return Timestamp(uint64(low) + (uint64(mid) << 12) + (uint64(hi) << 28)), nil +} + +// String parse helpers. +var ( + urnPrefix = []byte("urn:uuid:") + byteGroups = []int{8, 4, 4, 4, 12} +) + +// Nil is the nil UUID, as specified in RFC-4122, that has all 128 bits set to +// zero. +var Nil = UUID{} + +// Predefined namespace UUIDs. +var ( + NamespaceDNS = Must(FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")) + NamespaceURL = Must(FromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8")) + NamespaceOID = Must(FromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8")) + NamespaceX500 = Must(FromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8")) +) + +// IsNil returns if the UUID is equal to the nil UUID +func (u UUID) IsNil() bool { + return u == Nil +} + +// Version returns the algorithm version used to generate the UUID. +func (u UUID) Version() byte { + return u[6] >> 4 +} + +// Variant returns the UUID layout variant. +func (u UUID) Variant() byte { + switch { + case (u[8] >> 7) == 0x00: + return VariantNCS + case (u[8] >> 6) == 0x02: + return VariantRFC4122 + case (u[8] >> 5) == 0x06: + return VariantMicrosoft + case (u[8] >> 5) == 0x07: + fallthrough + default: + return VariantFuture + } +} + +// Bytes returns a byte slice representation of the UUID. +func (u UUID) Bytes() []byte { + return u[:] +} + +// String returns a canonical RFC-4122 string representation of the UUID: +// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. +func (u UUID) String() string { + buf := make([]byte, 36) + + hex.Encode(buf[0:8], u[0:4]) + buf[8] = '-' + hex.Encode(buf[9:13], u[4:6]) + buf[13] = '-' + hex.Encode(buf[14:18], u[6:8]) + buf[18] = '-' + hex.Encode(buf[19:23], u[8:10]) + buf[23] = '-' + hex.Encode(buf[24:], u[10:]) + + return string(buf) +} + +// Format implements fmt.Formatter for UUID values. +// +// The behavior is as follows: +// The 'x' and 'X' verbs output only the hex digits of the UUID, using a-f for 'x' and A-F for 'X'. +// The 'v', '+v', 's' and 'q' verbs return the canonical RFC-4122 string representation. +// The 'S' verb returns the RFC-4122 format, but with capital hex digits. +// The '#v' verb returns the "Go syntax" representation, which is a 16 byte array initializer. +// All other verbs not handled directly by the fmt package (like '%p') are unsupported and will return +// "%!verb(uuid.UUID=value)" as recommended by the fmt package. +func (u UUID) Format(f fmt.State, c rune) { + switch c { + case 'x', 'X': + s := hex.EncodeToString(u.Bytes()) + if c == 'X' { + s = strings.Map(toCapitalHexDigits, s) + } + _, _ = io.WriteString(f, s) + case 'v': + var s string + if f.Flag('#') { + s = fmt.Sprintf("%#v", [Size]byte(u)) + } else { + s = u.String() + } + _, _ = io.WriteString(f, s) + case 's', 'S': + s := u.String() + if c == 'S' { + s = strings.Map(toCapitalHexDigits, s) + } + _, _ = io.WriteString(f, s) + case 'q': + _, _ = io.WriteString(f, `"`+u.String()+`"`) + default: + // invalid/unsupported format verb + fmt.Fprintf(f, "%%!%c(uuid.UUID=%s)", c, u.String()) + } +} + +func toCapitalHexDigits(ch rune) rune { + // convert a-f hex digits to A-F + switch ch { + case 'a': + return 'A' + case 'b': + return 'B' + case 'c': + return 'C' + case 'd': + return 'D' + case 'e': + return 'E' + case 'f': + return 'F' + default: + return ch + } +} + +// SetVersion sets the version bits. +func (u *UUID) SetVersion(v byte) { + u[6] = (u[6] & 0x0f) | (v << 4) +} + +// SetVariant sets the variant bits. +func (u *UUID) SetVariant(v byte) { + switch v { + case VariantNCS: + u[8] = (u[8]&(0xff>>1) | (0x00 << 7)) + case VariantRFC4122: + u[8] = (u[8]&(0xff>>2) | (0x02 << 6)) + case VariantMicrosoft: + u[8] = (u[8]&(0xff>>3) | (0x06 << 5)) + case VariantFuture: + fallthrough + default: + u[8] = (u[8]&(0xff>>3) | (0x07 << 5)) + } +} + +// Must is a helper that wraps a call to a function returning (UUID, error) +// and panics if the error is non-nil. It is intended for use in variable +// initializations such as +// var packageUUID = uuid.Must(uuid.FromString("123e4567-e89b-12d3-a456-426655440000")) +func Must(u UUID, err error) UUID { + if err != nil { + panic(err) + } + return u +} diff --git a/vendor/modules.txt b/vendor/modules.txt new file mode 100644 index 0000000..c6ac032 --- /dev/null +++ b/vendor/modules.txt @@ -0,0 +1,3 @@ +# github.com/gofrs/uuid v4.3.0+incompatible +## explicit +github.com/gofrs/uuid