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
- Host: GitHub
- URL: https://github.com/catgoose/crooner
- Owner: catgoose
- Created: 2024-09-09T19:56:28.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-17T21:35:24.000Z (10 months ago)
- Last Synced: 2025-02-17T22:28:42.260Z (10 months ago)
- Topics: azure, echo-framework, go, golang
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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

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`