Skip to content

Commit

Permalink
signature: Make AuthorIdentity implement {Marshal,Unmarshal}Text (#92)
Browse files Browse the repository at this point in the history
This makes it easier to e.g. put AuthorIdentity values in configuration
and API structs and have the values properly marshaled to/from string.

Since the type has grown we extracted it to a file of its own to keep
the clutter in verifier.go down.
  • Loading branch information
magnusbaeck authored Jun 26, 2024
1 parent ad92e77 commit 4ed6edf
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 30 deletions.
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

0 comments on commit 4ed6edf

Please sign in to comment.