Skip to content

Commit

Permalink
Add www-Authenticate header (#2314)
Browse files Browse the repository at this point in the history
* add www authenticate header

* Update examples/internal/proto/examplepb/echo_service.proto

Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>

* implement add www-authenticate header

* regenerate

Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>
  • Loading branch information
momom-i and johanbrandhorst authored Sep 8, 2021
1 parent 8478702 commit f60dfa5
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 15 deletions.
78 changes: 78 additions & 0 deletions examples/internal/clients/echo/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,84 @@ paths:
description: "An unexpected error response."
schema:
$ref: "#/definitions/rpcStatus"
/v1/example/echo_unauthorized:
get:
tags:
- "EchoService"
summary: "EchoUnauthorized method receives a simple message and returns it.\
\ It must\nalways return a google.rpc.Code of `UNAUTHENTICATED` and a HTTP\
\ Status code\nof 401."
operationId: "EchoService_EchoUnauthorized"
parameters:
- name: "id"
in: "query"
description: "Id represents the message identifier."
required: false
type: "string"
x-exportParamName: "Id"
x-optionalDataType: "String"
- name: "num"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "Num"
x-optionalDataType: "String"
- name: "lineNum"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "LineNum"
x-optionalDataType: "String"
- name: "lang"
in: "query"
required: false
type: "string"
x-exportParamName: "Lang"
x-optionalDataType: "String"
- name: "status.progress"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "StatusProgress"
x-optionalDataType: "String"
- name: "status.note"
in: "query"
required: false
type: "string"
x-exportParamName: "StatusNote"
x-optionalDataType: "String"
- name: "en"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "En"
x-optionalDataType: "String"
- name: "no.progress"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "NoProgress"
x-optionalDataType: "String"
- name: "no.note"
in: "query"
required: false
type: "string"
x-exportParamName: "NoNote"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
schema:
$ref: "#/definitions/examplepbSimpleMessage"
default:
description: "An unexpected error response."
schema:
$ref: "#/definitions/rpcStatus"
definitions:
examplepbDynamicMessage:
type: "object"
Expand Down
147 changes: 147 additions & 0 deletions examples/internal/clients/echo/api_echo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,3 +1037,150 @@ func (a *EchoServiceApiService) EchoServiceEchoPatch(ctx context.Context, body E

return localVarReturnValue, localVarHttpResponse, nil
}

/*
EchoServiceApiService EchoUnauthorized method receives a simple message and returns it. It must always return a google.rpc.Code of &#x60;UNAUTHENTICATED&#x60; and a HTTP Status code of 401.
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param optional nil or *EchoServiceEchoUnauthorizedOpts - Optional Parameters:
* @param "Id" (optional.String) - Id represents the message identifier.
* @param "Num" (optional.String) -
* @param "LineNum" (optional.String) -
* @param "Lang" (optional.String) -
* @param "StatusProgress" (optional.String) -
* @param "StatusNote" (optional.String) -
* @param "En" (optional.String) -
* @param "NoProgress" (optional.String) -
* @param "NoNote" (optional.String) -
@return ExamplepbSimpleMessage
*/

type EchoServiceEchoUnauthorizedOpts struct {
Id optional.String
Num optional.String
LineNum optional.String
Lang optional.String
StatusProgress optional.String
StatusNote optional.String
En optional.String
NoProgress optional.String
NoNote optional.String
}

func (a *EchoServiceApiService) EchoServiceEchoUnauthorized(ctx context.Context, localVarOptionals *EchoServiceEchoUnauthorizedOpts) (ExamplepbSimpleMessage, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Get")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
localVarReturnValue ExamplepbSimpleMessage
)

// create path and map variables
localVarPath := a.client.cfg.BasePath + "/v1/example/echo_unauthorized"

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}

if localVarOptionals != nil && localVarOptionals.Id.IsSet() {
localVarQueryParams.Add("id", parameterToString(localVarOptionals.Id.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.Num.IsSet() {
localVarQueryParams.Add("num", parameterToString(localVarOptionals.Num.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.LineNum.IsSet() {
localVarQueryParams.Add("lineNum", parameterToString(localVarOptionals.LineNum.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.Lang.IsSet() {
localVarQueryParams.Add("lang", parameterToString(localVarOptionals.Lang.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.StatusProgress.IsSet() {
localVarQueryParams.Add("status.progress", parameterToString(localVarOptionals.StatusProgress.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.StatusNote.IsSet() {
localVarQueryParams.Add("status.note", parameterToString(localVarOptionals.StatusNote.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.En.IsSet() {
localVarQueryParams.Add("en", parameterToString(localVarOptionals.En.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NoProgress.IsSet() {
localVarQueryParams.Add("no.progress", parameterToString(localVarOptionals.NoProgress.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NoNote.IsSet() {
localVarQueryParams.Add("no.note", parameterToString(localVarOptionals.NoNote.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json"}

// set Content-Type header
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}

// to determine the Accept header
localVarHttpHeaderAccepts := []string{"application/json"}

// set Accept header
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return localVarReturnValue, nil, err
}

localVarHttpResponse, err := a.client.callAPI(r)
if err != nil || localVarHttpResponse == nil {
return localVarReturnValue, localVarHttpResponse, err
}

localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
localVarHttpResponse.Body.Close()
if err != nil {
return localVarReturnValue, localVarHttpResponse, err
}

if localVarHttpResponse.StatusCode < 300 {
// If we succeed, return the data, otherwise pass on to decode error.
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err == nil {
return localVarReturnValue, localVarHttpResponse, err
}
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericSwaggerError{
body: localVarBody,
error: localVarHttpResponse.Status,
}

if localVarHttpResponse.StatusCode == 200 {
var v ExamplepbSimpleMessage
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHttpResponse, newErr
}
newErr.model = v
return localVarReturnValue, localVarHttpResponse, newErr
}

if localVarHttpResponse.StatusCode == 0 {
var v RpcStatus
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHttpResponse, newErr
}
newErr.model = v
return localVarReturnValue, localVarHttpResponse, newErr
}

return localVarReturnValue, localVarHttpResponse, newErr
}

return localVarReturnValue, localVarHttpResponse, nil
}
33 changes: 32 additions & 1 deletion examples/internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ func TestEcho(t *testing.T) {
}
}

func TestEchoUnauthorized(t *testing.T) {
if testing.Short() {
t.Skip()
return
}
apiURL := "http://localhost:8088/v1/example/echo_unauthorized"
resp, err := http.Get(apiURL)
if err != nil {
t.Errorf("http.Get(%q) failed with %v; want success", apiURL, err)
return
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err)
return
}
msg := new(statuspb.Status)
if err := marshaler.Unmarshal(buf, msg); err != nil {
t.Errorf("marshaler.Unmarshal(%s, msg) failed with %v; want success", buf, err)
return
}

if got, want := resp.StatusCode, http.StatusUnauthorized; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
}

if value := resp.Header.Get("WWW-Authenticate"); value == "" {
t.Errorf("WWW-Authenticate header should not be empty")
}
}

func TestEchoPatch(t *testing.T) {
if testing.Short() {
t.Skip()
Expand Down Expand Up @@ -1566,7 +1598,6 @@ func TestNotImplemented(t *testing.T) {
t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err)
return
}

if got, want := resp.StatusCode, http.StatusNotImplemented; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
t.Logf("%s", buf)
Expand Down
41 changes: 27 additions & 14 deletions examples/internal/proto/examplepb/echo_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f60dfa5

Please sign in to comment.