sliproad/authentication/keycloak.go
Gabriel Simmer ce4ee64509
Minor linting fixes & move to CircleCI/UPX orb.
Some minor linting fixes and general style/scope changes, which should
have no impact on the overall application. Also moved to the offical
CircleCI UPX Orb, rather than my own (which is still maintained by me).
2020-04-24 09:01:53 +01:00

29 lines
917 B
Go

package authentication
import (
"fmt"
"github.com/Nerzal/gocloak/v5"
)
// AuthConfig contains the configuration for the IdP.
var AuthConfig map[string]string
// HasAuth checks the passed token against the IdP, and returns true
// if the IdP can return the user's info, false if not.
func HasAuth(accessToken string) (success bool) {
client := gocloak.NewClient(AuthConfig["provider_url"])
_, err := client.GetUserInfo(accessToken, AuthConfig["realm"])
if err != nil {
return false
}
return true
}
// GetLoginLink generates a redirect link to the IdP login page.
func GetLoginLink() (url string) {
baseString := "%v/auth/realms/%v/protocol/openid-connect/auth?client_id=account&response_mode=fragment&response_type=token&login=true&redirect_uri=%v/api/auth/callback"
authURL := fmt.Sprintf(baseString, AuthConfig["provider_url"], AuthConfig["realm"], AuthConfig["redirect_base_url"])
return authURL
}