Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lint errors and improve test #35

Merged
merged 3 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.37
version: v1.42
- name: Run gosec
run: go run github.com/securego/gosec/v2/cmd/gosec@latest ./...

Expand Down
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ run:
timeout: 1m
tests: false

linters-settings:
tagliatelle:
case:
use-field-name: true
rules:
json: snake

linters:
enable-all: true
7 changes: 6 additions & 1 deletion credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ func NewCredentialBytes(raw []byte) Credential {
}

func (cred credentialBytes) key() (*ecdsa.PrivateKey, error) {
return ecc.ReadPrivate(cred.raw)
key, err := ecc.ReadPrivate(cred.raw)
if err != nil {
return nil, fmt.Errorf("ecc: %w", err)
}

return key, nil
}

type credentialString struct {
Expand Down
25 changes: 20 additions & 5 deletions credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ func TestCredentialBytes_key(t *testing.T) {

cases := map[string]struct {
filename string
noErr bool
}{
"valid filename": {
filename: "revoked_private_key.p8",
noErr: true,
},
"invalid private key": {
filename: "invalid_private_key.p8",
noErr: false,
},
}

Expand All @@ -143,11 +149,20 @@ func TestCredentialBytes_key(t *testing.T) {
cred := NewCredentialBytes(raw)
key, err := cred.key()

if err != nil {
t.Errorf("want 'nil', got '%+v'", err)
}
if key == nil {
t.Error("want 'not nil', got 'nil'")
if c.noErr {
if err != nil {
t.Errorf("want 'nil', got '%+v'", err)
}
if key == nil {
t.Error("want 'not nil', got 'nil'")
}
} else {
if err == nil {
t.Error("want 'not nil', got 'nil'")
}
if key != nil {
t.Errorf("want 'nil', got '%+v'", key)
}
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions invalid_private_key.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN EC PRIVATE KEY-----
invalid
-----END EC PRIVATE KEY-----
8 changes: 7 additions & 1 deletion jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devicecheck
import (
"crypto/ecdsa"
"encoding/json"
"fmt"
"time"

jose "github.com/dvsekhvalnov/jose2go"
Expand Down Expand Up @@ -34,5 +35,10 @@ func (jwt jwt) generate(key *ecdsa.PrivateKey) (string, error) {
"kid": jwt.keyID,
}

return jose.Sign(string(claimsJSON), jose.ES256, key, jose.Headers(headers))
token, err := jose.Sign(string(claimsJSON), jose.ES256, key, jose.Headers(headers))
if err != nil {
return "", fmt.Errorf("jose: %w", err)
}

return token, nil
}
13 changes: 11 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ type Time struct {
const timeFormat = "2006-01"

func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Format(timeFormat))
b, err := json.Marshal(t.Format(timeFormat))
if err != nil {
return nil, fmt.Errorf("json: %w", err)
}

return b, nil
}

func (t *Time) UnmarshalJSON(b []byte) error {
Expand Down Expand Up @@ -78,5 +83,9 @@ func (client *Client) QueryTwoBits(ctx context.Context, deviceToken string, resu
return fmt.Errorf("devicecheck: %w", ErrBitStateNotFound)
}

return json.NewDecoder(strings.NewReader(respBody)).Decode(result)
if err := json.NewDecoder(strings.NewReader(respBody)).Decode(result); err != nil {
return fmt.Errorf("json: %w", err)
}

return nil
}