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

signature: Make AuthorIdentity implement {Marshal,Unmarshal}Text #92

Merged
merged 2 commits into from
Jun 26, 2024
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
66 changes: 66 additions & 0 deletions signature/authoridentity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright Axis Communications AB.
//
// For a full list of individual contributors, please see the commit history.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package signature

import (
"fmt"

"github.com/go-ldap/ldap"
)

// AuthorIdentity is a representation of the distinguished name
// in the meta.security.authorIdentity field of an Eiffel event.
// It can be compared to other values of the same type.
type AuthorIdentity struct {
dn *ldap.DN
original string
}

func NewAuthorIdentity(s string) (*AuthorIdentity, error) {
dn, err := ldap.ParseDN(s)
if err != nil {
return nil, fmt.Errorf("error parsing author identity %q: %w", s, err)
}
return &AuthorIdentity{
dn: dn,
original: s,
}, nil
}

// Equal returns true if the provided *AuthorIdentity is equal to this one,
// igoring differences in whitespace etc.
func (ai AuthorIdentity) Equal(other *AuthorIdentity) bool {
return ai.dn.Equal(other.dn)
}

func (ai AuthorIdentity) String() string {
return ai.original
}

func (ai AuthorIdentity) MarshalText() (text []byte, err error) {
return []byte(ai.original), nil
}

func (ai *AuthorIdentity) UnmarshalText(text []byte) error {
i, err := NewAuthorIdentity(string(text))
if err != nil {
return err
}
ai.dn = i.dn
ai.original = i.original
return nil
}
33 changes: 33 additions & 0 deletions signature/authoridentity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright Axis Communications AB.
//
// For a full list of individual contributors, please see the commit history.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package signature

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAuthorIdentity_UnmarshalText(t *testing.T) {
var data struct {
Identity *AuthorIdentity `json:"identity"`
}
require.NoError(t, json.Unmarshal([]byte(`{"identity": "CN=foo,DC=example,DC=com"}`), &data))
assert.Equal(t, "CN=foo,DC=example,DC=com", data.Identity.String())
}
30 changes: 0 additions & 30 deletions signature/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"errors"
"fmt"

"github.com/go-ldap/ldap"
"github.com/gowebpki/jcs"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
Expand All @@ -44,35 +43,6 @@ type PublicKeyLocator interface {
Locate(ctx context.Context, identity *AuthorIdentity) ([]crypto.PublicKey, error)
}

// AuthorIdentity is a representation of the distinguished name
// in the meta.security.authorIdentity field of an Eiffel event.
// It can be compared to other values of the same type.
type AuthorIdentity struct {
dn *ldap.DN
original string
}

func NewAuthorIdentity(s string) (*AuthorIdentity, error) {
dn, err := ldap.ParseDN(s)
if err != nil {
return nil, fmt.Errorf("error parsing author identity %q: %w", s, err)
}
return &AuthorIdentity{
dn: dn,
original: s,
}, nil
}

// Equal returns true if the provided *AuthorIdentity is equal to this one,
// igoring differences in whitespace etc.
func (ai *AuthorIdentity) Equal(other *AuthorIdentity) bool {
return ai.dn.Equal(other.dn)
}

func (ai *AuthorIdentity) String() string {
return ai.original
}

// Verifier can verify whether the signature of a given Eiffel event matches
// any of the keys known by the associated PublicKeyLocator.
type Verifier struct {
Expand Down