diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..0f93d93 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,23 @@ +name: go +on: [ push, pull_request ] + +jobs: + go: + runs-on: ubuntu-latest + strategy: + matrix: + go: [ 1.12, 1.13, 1.14, 1.15, 1.16, 1.17 ] + + steps: + - uses: actions/checkout@v2 + + - name: set up go@${{ matrix.go }} + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go }} + + - name: go@${{ matrix.go }} build + run: go build -v ./... + + - name: go@${{ matrix.go }} test + run: go test -v ./... diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..c2cec38 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,14 @@ +name: golangci-lint +on: [ push, pull_request ] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v2.5.2 + with: + # version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: latest # optional diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..ba426e9 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,46 @@ +run: + timeout: 60s +linters: + disable-all: true + enable: + - bodyclose + - deadcode + - dogsled + - errcheck + - gochecknoglobals + - gochecknoinits + - gocognit + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - revive + - gosec + - gosimple + - govet + - ineffassign + - misspell + - nakedret + - prealloc + - predeclared + - exportloopref + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace +issues: + exclude-rules: + - path: _test\.go + linters: + - errcheck + - funlen + - gocyclo + - gochecknoinits + - gochecknoglobals + - lll diff --git a/CHANGELOG.md b/CHANGELOG.md index fdb32f5..bf24bff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [v0.1.1] - 2022-01-25 ### Added - :memo: README: adds code verifier verification examples. +- :page_facing_up: LICENSE: adds MIT license. Fixes #4. +- :construction_worker: ci/cd: enables github actions. Fixes #3. +- :memo: README: adds go reference, go report and github action build badges. ### Fixed - :memo: README: fixes a couple of spelling misteaks. +- :rotating_light: pkce: fixes whitespace issues (wsl). +- :rotating_light: validation: simplifies if-return (revive). +- :rotating_light: errors: reduces line lengths (lll). ## [v0.1.0] - 2022-01-25 ### Added @@ -18,5 +25,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Verification for an incoming code verifier. - URL parameter key constants. -[Unreleased]: https://github.com/matthewhartstonge/pkce/compare/v0.1.0...HEAD +[Unreleased]: https://github.com/matthewhartstonge/pkce/compare/v0.1.1...HEAD +[v0.1.1]: https://github.com/matthewhartstonge/pkce/releases/tag/v0.1.1 [v0.1.0]: https://github.com/matthewhartstonge/pkce/releases/tag/v0.1.0 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c1e9a31 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Matthew Hartstonge + +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/README.md b/README.md index f60ca84..6f7e3db 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # pkce +[![Go Reference](https://pkg.go.dev/badge/github.com/matthewhartstonge/pkce.svg)](https://pkg.go.dev/github.com/matthewhartstonge/pkce) +[![Go Report Card](https://goreportcard.com/badge/github.com/matthewhartstonge/pkce)](https://goreportcard.com/report/github.com/matthewhartstonge/pkce) +[![Go Test](https://github.com/matthewhartstonge/pkce/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/matthewhartstonge/pkce/actions/workflows/go.yml) +[![golangci-lint](https://github.com/matthewhartstonge/pkce/actions/workflows/golangci-lint.yml/badge.svg?branch=main)](https://github.com/matthewhartstonge/pkce/actions/workflows/golangci-lint.yml) + `pkce` implements the client side of RFC 7636 "Proof Key for Code Exchange by OAuth Public Clients" (PKCE) to enable the generation of cryptographically secure and specification compliant code verifiers and code challenges. With :sparkles: no external dependencies :sparkles:. diff --git a/errors.go b/errors.go index 1127037..54a344a 100644 --- a/errors.go +++ b/errors.go @@ -18,9 +18,16 @@ var ( // ErrVerifierCharacters enforces character compliance with the unreserved // character set as specified in RFC 7636, 4.1. - ErrVerifierCharacters = errors.New("code verifier must only contain unreserved characters in the set {'" + unreserved + "'}") + ErrVerifierCharacters = fmt.Errorf( + "code verifier must only contain unreserved characters from the set: {'%s'}", + unreserved, + ) // ErrVerifierLength enforces compliance with the minimum and maximum // lengths as specified in RFC 7636, 4.1. - ErrVerifierLength = errors.New(fmt.Sprintf("code verifier must be between %d and %d characters long", verifierMinLen, verifierMaxLen)) + ErrVerifierLength = fmt.Errorf( + "code verifier must be between %d and %d characters long", + verifierMinLen, + verifierMaxLen, + ) ) diff --git a/pkce.go b/pkce.go index 89461c3..fe783a8 100644 --- a/pkce.go +++ b/pkce.go @@ -131,7 +131,6 @@ func VerifyCodeVerifier(method Method, codeVerifier string, codeChallenge string // received "code_verifier" and comparing it with the previously associated // "code_challenge", after first transforming it according to the // "code_challenge_method" method specified by the client. - switch method { case Plain: // If the "code_challenge_method" from Section 4.3 was "plain", they are @@ -172,6 +171,7 @@ func (k *Key) SetChallengeMethod(method Method) error { } k.challengeMethod = method + return nil } @@ -191,6 +191,7 @@ func (k *Key) setCodeVerifierLength(n int) error { } k.codeVerifierLen = n + return nil } diff --git a/validation.go b/validation.go index ad9f8c7..dc00fed 100644 --- a/validation.go +++ b/validation.go @@ -7,11 +7,7 @@ func validateCodeVerifier(verifier []byte) error { return err } - if err := validateCodeVerifierCharacters(verifier); err != nil { - return err - } - - return nil + return validateCodeVerifierCharacters(verifier) } // validateVerifierLen ensures the length of the code verifier is within the diff --git a/validation_test.go b/validation_test.go index 372287a..e1c7a37 100644 --- a/validation_test.go +++ b/validation_test.go @@ -9,6 +9,7 @@ func Test_validateVerifierLen(t *testing.T) { type args struct { n int } + tests := []struct { name string args args @@ -43,6 +44,7 @@ func Test_validateVerifierLen(t *testing.T) { wantErr: true, }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := validateVerifierLen(tt.args.n); (err != nil) != tt.wantErr { @@ -56,6 +58,7 @@ func Test_validVerifierChar(t *testing.T) { type args struct { c byte } + type validVerifierCharTest struct { name string args args