feat: add username and password login (#418)

* feat: add username and password login

* fix: make sure cookie is not empty

* chore: go mod tidy

* fix: prevent local config from influencing tests

* fix: small cleanup on error handling

* fix: remove unnecessary trim
This commit is contained in:
Jeffrey Smith II
2022-07-28 10:53:19 -04:00
committed by GitHub
parent 182303e31d
commit f34e6a888f
11 changed files with 313 additions and 4 deletions

203
api/api_signin.gen.go Normal file
View File

@ -0,0 +1,203 @@
/*
* Subset of Influx API covered by Influx CLI
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 2.0.0
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package api
import (
_context "context"
_fmt "fmt"
_io "io"
_nethttp "net/http"
_neturl "net/url"
)
// Linger please
var (
_ _context.Context
)
type SigninApi interface {
/*
* PostSignin Create a user session.
* Authenticates ***Basic Auth*** credentials for a user. If successful, creates a new UI session for the user.
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @return ApiPostSigninRequest
*/
PostSignin(ctx _context.Context) ApiPostSigninRequest
/*
* PostSigninExecute executes the request
*/
PostSigninExecute(r ApiPostSigninRequest) error
/*
* PostSigninExecuteWithHttpInfo executes the request with HTTP response info returned. The response body is not
* available on the returned HTTP response as it will have already been read and closed; access to the response body
* content should be achieved through the returned response model if applicable.
*/
PostSigninExecuteWithHttpInfo(r ApiPostSigninRequest) (*_nethttp.Response, error)
}
// SigninApiService SigninApi service
type SigninApiService service
type ApiPostSigninRequest struct {
ctx _context.Context
ApiService SigninApi
zapTraceSpan *string
}
func (r ApiPostSigninRequest) ZapTraceSpan(zapTraceSpan string) ApiPostSigninRequest {
r.zapTraceSpan = &zapTraceSpan
return r
}
func (r ApiPostSigninRequest) GetZapTraceSpan() *string {
return r.zapTraceSpan
}
func (r ApiPostSigninRequest) Execute() error {
return r.ApiService.PostSigninExecute(r)
}
func (r ApiPostSigninRequest) ExecuteWithHttpInfo() (*_nethttp.Response, error) {
return r.ApiService.PostSigninExecuteWithHttpInfo(r)
}
/*
* PostSignin Create a user session.
* Authenticates ***Basic Auth*** credentials for a user. If successful, creates a new UI session for the user.
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @return ApiPostSigninRequest
*/
func (a *SigninApiService) PostSignin(ctx _context.Context) ApiPostSigninRequest {
return ApiPostSigninRequest{
ApiService: a,
ctx: ctx,
}
}
/*
* Execute executes the request
*/
func (a *SigninApiService) PostSigninExecute(r ApiPostSigninRequest) error {
_, err := a.PostSigninExecuteWithHttpInfo(r)
return err
}
/*
* ExecuteWithHttpInfo executes the request with HTTP response info returned. The response body is not available on the
* returned HTTP response as it will have already been read and closed; access to the response body content should be
* achieved through the returned response model if applicable.
*/
func (a *SigninApiService) PostSigninExecuteWithHttpInfo(r ApiPostSigninRequest) (*_nethttp.Response, error) {
var (
localVarHTTPMethod = _nethttp.MethodPost
localVarPostBody interface{}
localVarFormFileName string
localVarFileName string
localVarFileBytes []byte
)
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "SigninApiService.PostSignin")
if err != nil {
return nil, GenericOpenAPIError{error: err.Error()}
}
localVarPath := localBasePath + "/api/v2/signin"
localVarHeaderParams := make(map[string]string)
localVarQueryParams := _neturl.Values{}
localVarFormParams := _neturl.Values{}
// to determine the Content-Type header
localVarHTTPContentTypes := []string{}
// 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
}
if r.zapTraceSpan != nil {
localVarHeaderParams["Zap-Trace-Span"] = parameterToString(*r.zapTraceSpan, "")
}
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}
localVarHTTPResponse, err := a.client.callAPI(req)
if err != nil || localVarHTTPResponse == nil {
return localVarHTTPResponse, err
}
newErr := GenericOpenAPIError{
buildHeader: localVarHTTPResponse.Header.Get("X-Influxdb-Build"),
}
if localVarHTTPResponse.StatusCode >= 300 {
body, err := GunzipIfNeeded(localVarHTTPResponse)
if err != nil {
body.Close()
newErr.error = err.Error()
return localVarHTTPResponse, newErr
}
localVarBody, err := _io.ReadAll(body)
body.Close()
if err != nil {
newErr.error = err.Error()
return localVarHTTPResponse, newErr
}
newErr.body = localVarBody
newErr.error = localVarHTTPResponse.Status
if localVarHTTPResponse.StatusCode == 401 {
var v Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = _fmt.Sprintf("%s: %s", newErr.Error(), err.Error())
return localVarHTTPResponse, newErr
}
v.SetMessage(_fmt.Sprintf("%s: %s", newErr.Error(), v.GetMessage()))
newErr.model = &v
return localVarHTTPResponse, newErr
}
if localVarHTTPResponse.StatusCode == 403 {
var v Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = _fmt.Sprintf("%s: %s", newErr.Error(), err.Error())
return localVarHTTPResponse, newErr
}
v.SetMessage(_fmt.Sprintf("%s: %s", newErr.Error(), v.GetMessage()))
newErr.model = &v
return localVarHTTPResponse, newErr
}
var v Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = _fmt.Sprintf("%s: %s", newErr.Error(), err.Error())
return localVarHTTPResponse, newErr
}
v.SetMessage(_fmt.Sprintf("%s: %s", newErr.Error(), v.GetMessage()))
newErr.model = &v
return localVarHTTPResponse, newErr
}
return localVarHTTPResponse, nil
}

View File

@ -90,6 +90,8 @@ type APIClient struct {
SetupApi SetupApi
SigninApi SigninApi
StacksApi StacksApi
TasksApi TasksApi
@ -143,6 +145,7 @@ func NewAPIClient(cfg *Configuration) *APIClient {
c.RestoreApi = (*RestoreApiService)(&c.common)
c.SecretsApi = (*SecretsApiService)(&c.common)
c.SetupApi = (*SetupApiService)(&c.common)
c.SigninApi = (*SigninApiService)(&c.common)
c.StacksApi = (*StacksApiService)(&c.common)
c.TasksApi = (*TasksApiService)(&c.common)
c.TelegrafsApi = (*TelegrafsApiService)(&c.common)

View File

@ -11,6 +11,7 @@ type ConfigParams struct {
Host *url.URL
UserAgent string
Token *string
Cookie *string
TraceId *string
AllowInsecureTLS bool
Debug bool
@ -26,8 +27,10 @@ func NewAPIConfig(params ConfigParams) *Configuration {
apiConfig.Scheme = params.Host.Scheme
apiConfig.UserAgent = params.UserAgent
apiConfig.HTTPClient = &http.Client{Transport: clientTransport}
if params.Token != nil {
if params.Token != nil && *params.Token != "" {
apiConfig.DefaultHeader["Authorization"] = fmt.Sprintf("Token %s", *params.Token)
} else if params.Cookie != nil && *params.Cookie != "" {
apiConfig.DefaultHeader["Cookie"] = fmt.Sprintf("influxdb-oss-session=%s", *params.Cookie)
}
if params.TraceId != nil {
// NOTE: This is circumventing our codegen. If the header we use for tracing ever changes,

View File

@ -129,6 +129,8 @@ paths:
$ref: "./openapi/src/svc/invocable-scripts/paths/scripts_scriptID.yml"
/api/v2/scripts/{scriptID}/invoke:
$ref: "./overrides/paths/scripts_scriptID_invoke.yml"
/api/v2/signin:
$ref: "./openapi/src/common/paths/signin.yml"
components:
parameters:
TraceSpan: