Skip to content

Commit

Permalink
api: fix ExpiredAt format (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
h44z committed Feb 17, 2025
1 parent 4316327 commit fc712eb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 61 deletions.
8 changes: 1 addition & 7 deletions docs/documentation/rest-api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ definitions:
description: Error message.
type: string
type: object
models.ExpiryDate:
properties:
time.Time:
type: string
type: object
models.Interface:
properties:
Addresses:
Expand Down Expand Up @@ -306,10 +301,9 @@ definitions:
- $ref: '#/definitions/models.ConfigOption-string'
description: EndpointPublicKey is the endpoint public key.
ExpiresAt:
allOf:
- $ref: '#/definitions/models.ExpiryDate'
description: ExpiresAt is the expiry date of the peer in YYYY-MM-DD format.
An expired peer is not able to connect.
type: string
ExtraAllowedIPs:
description: ExtraAllowedIPs is a list of additional allowed IP subnets for
the peer. These allowed IP subnets are added on the server side.
Expand Down
14 changes: 1 addition & 13 deletions internal/app/api/core/assets/doc/v1_swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1471,14 +1471,6 @@
}
}
},
"models.ExpiryDate": {
"type": "object",
"properties": {
"time.Time": {
"type": "string"
}
}
},
"models.Interface": {
"type": "object",
"required": [
Expand Down Expand Up @@ -1798,11 +1790,7 @@
},
"ExpiresAt": {
"description": "ExpiresAt is the expiry date of the peer in YYYY-MM-DD format. An expired peer is not able to connect.",
"allOf": [
{
"$ref": "#/definitions/models.ExpiryDate"
}
]
"type": "string"
},
"ExtraAllowedIPs": {
"description": "ExtraAllowedIPs is a list of additional allowed IP subnets for the peer. These allowed IP subnets are added on the server side.",
Expand Down
8 changes: 1 addition & 7 deletions internal/app/api/core/assets/doc/v1_swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ definitions:
description: Error message.
type: string
type: object
models.ExpiryDate:
properties:
time.Time:
type: string
type: object
models.Interface:
properties:
Addresses:
Expand Down Expand Up @@ -306,10 +301,9 @@ definitions:
- $ref: '#/definitions/models.ConfigOption-string'
description: EndpointPublicKey is the endpoint public key.
ExpiresAt:
allOf:
- $ref: '#/definitions/models.ExpiryDate'
description: ExpiresAt is the expiry date of the peer in YYYY-MM-DD format.
An expired peer is not able to connect.
type: string
ExtraAllowedIPs:
description: ExtraAllowedIPs is a list of additional allowed IP subnets for
the peer. These allowed IP subnets are added on the server side.
Expand Down
49 changes: 15 additions & 34 deletions internal/app/api/v1/models/models_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,7 @@ import (
"github.com/h44z/wg-portal/internal/domain"
)

const ExpiryDateTimeLayout = "\"2006-01-02\""

type ExpiryDate struct {
*time.Time
}

// UnmarshalJSON will unmarshal using 2006-01-02 layout
func (d *ExpiryDate) UnmarshalJSON(b []byte) error {
if len(b) == 0 || string(b) == "null" || string(b) == "\"\"" {
return nil
}
parsed, err := time.Parse(ExpiryDateTimeLayout, string(b))
if err != nil {
return err
}

if !parsed.IsZero() {
d.Time = &parsed
}
return nil
}

// MarshalJSON will marshal using 2006-01-02 layout
func (d *ExpiryDate) MarshalJSON() ([]byte, error) {
if d == nil || d.Time == nil {
return []byte("null"), nil
}

s := d.Format(ExpiryDateTimeLayout)
return []byte(s), nil
}
const ExpiryDateTimeLayout = "2006-01-02"

// Peer represents a WireGuard peer entry.
type Peer struct {
Expand All @@ -54,7 +24,7 @@ type Peer struct {
// DisabledReason is the reason why the peer has been disabled.
DisabledReason string `json:"DisabledReason" binding:"required_if=Disabled true" example:"This is a reason why the peer has been disabled."`
// ExpiresAt is the expiry date of the peer in YYYY-MM-DD format. An expired peer is not able to connect.
ExpiresAt ExpiryDate `json:"ExpiresAt,omitempty" binding:"omitempty,datetime=2006-01-02"`
ExpiresAt string `json:"ExpiresAt,omitempty" binding:"omitempty,datetime=2006-01-02"`
// Notes is a note field for peers.
Notes string `json:"Notes" example:"This is a note for the peer."`

Expand Down Expand Up @@ -105,14 +75,19 @@ type Peer struct {
}

func NewPeer(src *domain.Peer) *Peer {
expiresAt := ""
if src.ExpiresAt != nil && !src.ExpiresAt.IsZero() {
expiresAt = src.ExpiresAt.Format(ExpiryDateTimeLayout)
}

return &Peer{
Identifier: string(src.Identifier),
DisplayName: src.DisplayName,
UserIdentifier: string(src.UserIdentifier),
InterfaceIdentifier: string(src.InterfaceIdentifier),
Disabled: src.IsDisabled(),
DisabledReason: src.DisabledReason,
ExpiresAt: ExpiryDate{src.ExpiresAt},
ExpiresAt: expiresAt,
Notes: src.Notes,
Endpoint: ConfigOptionFromDomain(src.Endpoint),
EndpointPublicKey: ConfigOptionFromDomain(src.EndpointPublicKey),
Expand Down Expand Up @@ -150,6 +125,12 @@ func NewDomainPeer(src *Peer) *domain.Peer {
now := time.Now()

cidrs, _ := domain.CidrsFromArray(src.Addresses)
var expiresAt *time.Time
if src.ExpiresAt != "" {
if t, err := time.Parse(ExpiryDateTimeLayout, src.ExpiresAt); err == nil {
expiresAt = &t
}
}

res := &domain.Peer{
BaseModel: domain.BaseModel{},
Expand All @@ -165,7 +146,7 @@ func NewDomainPeer(src *Peer) *domain.Peer {
InterfaceIdentifier: domain.InterfaceIdentifier(src.InterfaceIdentifier),
Disabled: nil, // set below
DisabledReason: src.DisabledReason,
ExpiresAt: src.ExpiresAt.Time,
ExpiresAt: expiresAt,
Notes: src.Notes,
Interface: domain.PeerInterfaceConfig{
KeyPair: domain.KeyPair{
Expand Down

0 comments on commit fc712eb

Please sign in to comment.