[Go] How setup credentials like cookie in go Clients / Add auth endpoint which are not in openapi-spec file #4795
Unanswered
FlorianBorn
asked this question in
Q&A
Replies: 2 comments
-
Hi @FlorianBorn Doing so will circumvent the authentication infrastructure for kiota clients. So you can pass an anonymous authentication provider (assuming there's no other authentication scheme in play) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here's a solution for handling authentication with a Go client using Kiota when the authentication endpoint isn't in the OpenAPI spec: package main
import (
"context"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
kiota "github.com/microsoft/kiota-go"
"github.com/microsoft/kiota-go/transport"
)
// CustomAuthProvider implements the kiota.AuthenticationProvider interface
type CustomAuthProvider struct {
cookieJar *http.CookieJar
authBaseURL string
credentials map[string]string
}
// Authenticate method to handle authentication and cookie management
func (c *CustomAuthProvider) Authenticate(ctx context.Context, request *http.Request) error {
// If we already have cookies, add them to the request
if c.cookieJar != nil {
cookies := (*c.cookieJar).Cookies(request.URL)
for _, cookie := range cookies {
request.AddCookie(cookie)
}
}
return nil
}
// AuthenticateWithCookies performs the actual login and stores cookies
func (c *CustomAuthProvider) AuthenticateWithCookies() error {
// Create a new cookie jar if not exists
if c.cookieJar == nil {
jar, err := cookiejar.New(nil)
if err != nil {
return fmt.Errorf("failed to create cookie jar: %v", err)
}
c.cookieJar = &jar
}
// Prepare login request
loginURL := c.authBaseURL + "/login"
loginData := url.Values{}
for key, value := range c.credentials {
loginData.Set(key, value)
}
// Create HTTP client with the cookie jar
client := &http.Client{
Jar: *c.cookieJar,
}
// Perform login request
resp, err := client.PostForm(loginURL, loginData)
if err != nil {
return fmt.Errorf("login request failed: %v", err)
}
defer resp.Body.Close()
// Check login response status
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("authentication failed with status: %d", resp.StatusCode)
}
return nil
}
// CreateKiotaClient sets up a Kiota client with custom authentication
func CreateKiotaClient(authBaseURL string, credentials map[string]string) (*kiota.RequestAdapter, error) {
// Create custom auth provider
authProvider := &CustomAuthProvider{
authBaseURL: authBaseURL,
credentials: credentials,
}
// Perform authentication to get cookies
err := authProvider.AuthenticateWithCookies()
if err != nil {
return nil, fmt.Errorf("failed to authenticate: %v", err)
}
// Create middleware chain
middlewares := []kiota.RequestMiddleware{
transport.NewAuthenticationMiddleware(authProvider),
}
// Create request adapter
return kiota.NewRequestAdapter(
kiota.NewDefaultAuthenticationProvider(),
kiota.WithMiddleware(middlewares...),
)
}
func main() {
// Example usage
credentials := map[string]string{
"username": "your_username",
"password": "your_password",
}
// Create Kiota client with authentication
requestAdapter, err := CreateKiotaClient("https://your-auth-base-url.com", credentials)
if err != nil {
fmt.Println("Failed to create client:", err)
return
}
// Now use requestAdapter to make authenticated requests
// client := yourapi.NewYourAPIClient(requestAdapter)
}
``` |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
we have just created our first Go client using Kioata. Now we are facing the problem that the application the client is supposed to address expects authentication via a cookie + the authentication endpoint itself is not included in the OpenAPI spec file.
The auth cookie must be sent with every request.
I think regarding the auth cookie, we would need two things:
Can anyone help here, or know if this is currently even possible? Is there a way to implement the auth endpoint afterwards (or even beforehand)?
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions