You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to protect the operation, we may implement service-to-service authentication via the Authorization header. Meanwhile, there is no corresponding argument in GetUser to be mapped to.
So how to represent this Authorization header in kok's HTTP annotation?
Proposed Solution
Add support for the blank identifier __ (double underscore, similar to Go's blank identifier) in @kok(param):
And here is the possible codec.go for the Authorization header:
import (
"fmt""github.com/RussellLuo/kok/pkg/codec/httpcodec"
)
const (
authPrivateKey="AUTH_PRIVATE_KEY"
)
// AuthCodec is used to encode and decode the `Authorization` header. It can be reused wherever needed.typeAuthCodecstruct{}
func (cAuthCodec) Decode(in []string, outinterface{}) error {
// NOTE: never use out, which is nil here.iflen(in) ==0||in[0] !=authPrivateKey {
returnfmt.Errorf("authentication failed")
}
returnnil
}
func (cAuthCodec) Encode(ininterface{}) (out []string) {
// NOTE: never use in, which is nil here.return []string{authPrivateKey}
}
funcNewCodecs() *httpcodec.DefaultCodecs {
// Use AuthCodec to encode and decode the argument named "__", if exists,// for the operation named "GetUser".returnhttpcodec.NewDefaultCodecs(nil,
httpcodec.Op("GetUser", httpcodec.NewPatcher(httpcodec.JSON{}).Param("__", AuthCodec{})))
}
Multiple blank identifiers
In complicated cases, where multiple blank identifiers are involved and need to be differentiated from one another (typically for applying different encoders and decoders), we can use meaningful names, each with a double underscore prefix __ (e.g. __auth).
In summary, any name with a double underscore prefix __ is a valid blank identifier:
__
__auth
__other
The text was updated successfully, but these errors were encountered:
Problem
Sometimes, an HTTP request parameter is only used in the Transport/Endpoint layer, and no further argument binding (in the Service layer) is needed.
Taking the following
GetUser
operation as an example:In order to protect the operation, we may implement service-to-service authentication via the
Authorization
header. Meanwhile, there is no corresponding argument inGetUser
to be mapped to.So how to represent this
Authorization
header in kok's HTTP annotation?Proposed Solution
Add support for the blank identifier
__
(double underscore, similar to Go's blank identifier) in@kok(param)
:And here is the possible
codec.go
for theAuthorization
header:Multiple blank identifiers
In complicated cases, where multiple blank identifiers are involved and need to be differentiated from one another (typically for applying different encoders and decoders), we can use meaningful names, each with a double underscore prefix
__
(e.g.__auth
).In summary, any name with a double underscore prefix
__
is a valid blank identifier:__
__auth
__other
The text was updated successfully, but these errors were encountered: