Skip to content

Instantly share code, notes, and snippets.

@vosst
Last active September 11, 2019 09:58
Show Gist options
  • Save vosst/0761884ca38582d988f4d5dcc1ef0ba5 to your computer and use it in GitHub Desktop.
Save vosst/0761884ca38582d988f4d5dcc1ef0ba5 to your computer and use it in GitHub Desktop.
Programmatic login procedure for AirMap's IAM
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
oidc "github.com/coreos/go-oidc"
"golang.org/x/oauth2"
)
// This snippet acquires a token for a given username/password combination.
func main() {
var (
ctx = context.Background()
clientID = flag.String("client-id", "", "the application's client ID")
authHost = flag.String("auth-host", "https://che.auth.airmap.com", "the auth host")
username = flag.String("username", "", "the username")
password = flag.String("password", "", "the password")
)
flag.Parse()
endpoint := oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/realms/airmap/protocol/openid-connect/auth", *authHost),
TokenURL: fmt.Sprintf("%s/realms/airmap/protocol/openid-connect/token", *authHost),
}
config := oauth2.Config{
ClientID: *clientID,
Endpoint: endpoint,
RedirectURL: "http://oauth2/airmap",
Scopes: []string{
oidc.ScopeOpenID,
"email",
// Add target audience ${AUDIENCE} here in the format aud:${AUDIENCE}
"dss.write.identification_service_areas",
"dss.read.identification_service_areas",
},
}
token, err := config.PasswordCredentialsToken(ctx, *username, *password)
if err != nil {
panic(err)
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
encoder.Encode(token)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment