An open API service indexing awesome lists of open source software.

https://github.com/catgoose/crooner

Crooner is a golang module for authenticating with an Azure app registration
https://github.com/catgoose/crooner

azure echo-framework go golang

Last synced: 9 months ago
JSON representation

Crooner is a golang module for authenticating with an Azure app registration

Awesome Lists containing this project

README

          

# crooner

- [crooner](#crooner)
- [About](#about)
- [Installation](#installation)
- [Usage](#usage)
- [Todo](#todo)

Crooner is a golang library for authenticating with an Azure app registration

![image](https://github.com/catgoose/screenshots/blob/fb17ed7cd8e989691447b0e7a755d93a677abbfd/crooner/crooner.png)

Ever want to authenticate with Azure in your Go project but MSAL has no
examples for a hosted HTTP service:

## About

Crooner is a Go library designed to simplify Azure authentication for Go-based
HTTP servers. It's built to address a specific use case of integrating Azure's
not intended to be a fully general-purpose OIDC authorization solution but focuses
on the specific needs of Azure app registrations.

If you're using a different framework or want to implement a more general solution
with the standard Go http library, PRs are welcome!

## Installation

```bash
go get github.com/catgoose/crooner@v1.0.0
```

## Usage

```go
import (
"context"
"log"
"net/http"
"os"

"github.com/catgoose/crooner"
"github.com/gorilla/sessions"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)

func getAzureConfig() *crooner.AuthConfigParams {
return &crooner.AuthConfigParams{
TenantID: os.Getenv("AZURE_TENANT_ID"),
ClientID: os.Getenv("AZURE_CLIENT_ID"),
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
RedirectURL: os.Getenv("AZURE_REDIRECT_URL"),
LogoutURLRedirect: os.Getenv("AZURE_LOGOUT_REDIRECT_URL"),
LoginURLRedirect: os.Getenv("AZURE_LOGIN_REDIRECT_URL"),
AuthRoutes: &crooner.AuthRoutes{
Login: "/login",
Logout: "/logout",
Callback: "/callback",
// optional, routes exempt from auth middleware
AuthExempt: []string{"/profile", "/about"}
},
// optional, additional scopes to request
AdditionalScopes: []string{"User"},
},
// Map of session values to claims to store in session.
// Use c.get("value") to retrieve claim
SessionValueClaims: []map[string]string{
{"azureId": "oid"},
},
}

func main() {
e := echo.New()

e.Use(middleware.Logger())
e.Use(middleware.Recover())

// Initialize Crooner authentication
params := getAzureConfig()

secret := os.Getenv("SESSION_SECRET")
store := sessions.NewCookieStore([]byte(secret))
// CroonerConfig.SessionStore must be set
params.CroonerConfig.SessionStore = store
e.Use(session.Middleware(store))

ctx := context.Background()
err := crooner.NewAuthConfig(ctx, e, params)
if err != nil {
log.Fatalf("Failed to initialize Crooner: %v", err)
}

// Read azureId from session
sess, err := session.Get("crooner-auth", c)
if err != nil {
return HandleError(c, http.StatusInternalServerError, "failed to retrieve session", err)
}
azureId, ok := sess.Values["azureId"].(string)
if !ok || azureId == "" {
return HandleError(c, http.StatusUnauthorized, "user not authenticated", nil)
}
}
```

Note: Remember in Azure app registration to enable `ID tokens` to be issued

## Todo

- [ ] Create interface for saving session value to allow for other stores to
store more information other than `azureId`