-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
signature: Make AuthorIdentity implement {Marshal,Unmarshal}Text (#92)
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
1 parent
ad92e77
commit 4ed6edf
Showing
3 changed files
with
99 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters