added updated code for core wiring

1. Removed tracing
2. Skipped Registration
This commit is contained in:
MohitKatare-protean
2025-03-25 21:06:34 +05:30
parent 519cca19af
commit ec558558c5
87 changed files with 9279 additions and 711 deletions

128
pkg/model/error.go Normal file
View File

@@ -0,0 +1,128 @@
package model
import (
"fmt"
"net/http"
"strings"
)
// Error represents an error response.
type Error struct {
Code string `json:"code"`
Paths string `json:"paths,omitempty"`
Message string `json:"message"`
}
// Error implements the error interface for the Error struct.
func (e *Error) Error() string {
return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message)
}
// SchemaValidationErr represents a collection of schema validation failures.
type SchemaValidationErr struct {
Errors []Error
}
// Error implements the error interface for SchemaValidationErr.
func (e *SchemaValidationErr) Error() string {
var errorMessages []string
for _, err := range e.Errors {
errorMessages = append(errorMessages, fmt.Sprintf("%s: %s", err.Paths, err.Message))
}
return strings.Join(errorMessages, "; ")
}
// BecknError converts SchemaValidationErr into a Beckn-compliant Error response.
func (e *SchemaValidationErr) BecknError() *Error {
if len(e.Errors) == 0 {
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Message: "Schema validation error.",
}
}
// Collect all error paths and messages
var paths []string
var messages []string
for _, err := range e.Errors {
if err.Paths != "" {
paths = append(paths, err.Paths)
}
messages = append(messages, err.Message)
}
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Paths: strings.Join(paths, ";"),
Message: strings.Join(messages, "; "),
}
}
// SignValidationErr represents an error that occurs during signature validation.
type SignValidationErr struct {
error
}
// NewSignValidationErrf creates a new SignValidationErr with a formatted message.
func NewSignValidationErrf(format string, a ...any) *SignValidationErr {
return &SignValidationErr{fmt.Errorf(format, a...)}
}
// NewSignValidationErr creates a new SignValidationErr from an existing error.
func NewSignValidationErr(e error) *SignValidationErr {
return &SignValidationErr{e}
}
// BecknError converts SignValidationErr into a Beckn-compliant Error response.
func (e *SignValidationErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusUnauthorized),
Message: "Signature Validation Error: " + e.Error(),
}
}
// BadReqErr represents an error related to a bad request.
type BadReqErr struct {
error
}
// NewBadReqErr creates a new BadReqErr from an existing error.
func NewBadReqErr(err error) *BadReqErr {
return &BadReqErr{err}
}
// NewBadReqErrf creates a new BadReqErr with a formatted message.
func NewBadReqErrf(format string, a ...any) *BadReqErr {
return &BadReqErr{fmt.Errorf(format, a...)}
}
// BecknError converts BadReqErr into a Beckn-compliant Error response.
func (e *BadReqErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusBadRequest),
Message: "BAD Request: " + e.Error(),
}
}
// NotFoundErr represents an error for a missing resource or endpoint.
type NotFoundErr struct {
error
}
// NewNotFoundErr creates a new NotFoundErr from an existing error.
func NewNotFoundErr(err error) *NotFoundErr {
return &NotFoundErr{err}
}
// NewNotFoundErrf creates a new NotFoundErr with a formatted message.
func NewNotFoundErrf(format string, a ...any) *NotFoundErr {
return &NotFoundErr{fmt.Errorf(format, a...)}
}
// BecknError converts NotFoundErr into a Beckn-compliant Error response.
func (e *NotFoundErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusNotFound),
Message: "Endpoint not found: " + e.Error(),
}
}

132
pkg/model/model.go Normal file
View File

@@ -0,0 +1,132 @@
package model
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// Subscriber represents a unique operational configuration of a trusted platform on a network.
type Subscriber struct {
SubscriberID string `json:"subscriber_id"`
URL string `json:"url" format:"uri"`
Type string `json:"type" enum:"BAP,BPP,BG"`
Domain string `json:"domain"`
}
// Subscription represents subscription details of a network participant.
type Subscription struct {
Subscriber `json:",inline"`
KeyID string `json:"key_id" format:"uuid"`
SigningPublicKey string `json:"signing_public_key"`
EncrPublicKey string `json:"encr_public_key"`
ValidFrom time.Time `json:"valid_from" format:"date-time"`
ValidUntil time.Time `json:"valid_until" format:"date-time"`
Status string `json:"status" enum:"INITIATED,UNDER_SUBSCRIPTION,SUBSCRIBED,EXPIRED,UNSUBSCRIBED,INVALID_SSL"`
Created time.Time `json:"created" format:"date-time"`
Updated time.Time `json:"updated" format:"date-time"`
Nonce string
}
// Authorization-related constants for headers.
const (
AuthHeaderSubscriber string = "Authorization"
AuthHeaderGateway string = "X-Gateway-Authorization"
UnaAuthorizedHeaderSubscriber string = "WWW-Authenticate"
UnaAuthorizedHeaderGateway string = "Proxy-Authenticate"
)
// MsgIDKey represents the key for the message ID.
const MsgIDKey = "message_id"
// Role defines different roles in the network.
type Role string
const (
// RoleBAP represents a Buyer App Participant.
RoleBAP Role = "bap"
// RoleBPP represents a Buyer Platform Participant.
RoleBPP Role = "bpp"
// RoleGateway represents a Network Gateway.
RoleGateway Role = "gateway"
// RoleRegistery represents a Registry Service.
RoleRegistery Role = "registery"
)
// validRoles ensures only allowed values are accepted
var validRoles = map[Role]bool{
RoleBAP: true,
RoleBPP: true,
RoleGateway: true,
RoleRegistery: true,
}
// UnmarshalYAML implements custom YAML unmarshalling for Role to ensure only valid values are accepted.
func (r *Role) UnmarshalYAML(unmarshal func(interface{}) error) error {
var roleName string
if err := unmarshal(&roleName); err != nil {
return err
}
role := Role(roleName)
if !validRoles[role] {
return fmt.Errorf("invalid Role: %s", roleName)
}
*r = role
return nil
}
// Route represents a network route for message processing.
type Route struct {
Type string
URL *url.URL
Publisher string
}
// StepContext holds context information for a request processing step.
type StepContext struct {
context.Context
Request *http.Request
Body []byte
Route *Route
SubID string
Role Role
RespHeader http.Header
}
// WithContext updates the context in StepContext while keeping other fields unchanged.
func (ctx *StepContext) WithContext(newCtx context.Context) {
ctx.Context = newCtx // Update the existing context, keeping all other fields unchanged.
}
// Status represents the status of an acknowledgment.
type Status string
const (
// StatusACK indicates a successful acknowledgment.
StatusACK Status = "ACK"
// StatusNACK indicates a negative acknowledgment.
StatusNACK Status = "NACK"
)
// Ack represents an acknowledgment response.
type Ack struct {
Status Status `json:"status"` // ACK or NACK
}
// Message represents the message object in the response.
type Message struct {
Ack Ack `json:"ack"`
Error *Error `json:"error,omitempty"`
}
// Response represents the main response structure.
type Response struct {
Message Message `json:"message"`
}