|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "dario.cat/mergo" |
| 5 | + "github.com/hashicorp/go-version" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + "github.com/keycloak/terraform-provider-keycloak/keycloak" |
| 8 | + "github.com/keycloak/terraform-provider-keycloak/keycloak/types" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceKeycloakOidcGithubIdentityProvider() *schema.Resource { |
| 12 | + oidcGithubSchema := map[string]*schema.Schema{ |
| 13 | + "alias": { |
| 14 | + Type: schema.TypeString, |
| 15 | + Optional: true, |
| 16 | + Computed: true, |
| 17 | + Description: "The alias uniquely identifies an identity provider and it is also used to build the redirect uri. In case of github this is computed and always github", |
| 18 | + }, |
| 19 | + "display_name": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + Computed: true, |
| 23 | + Description: "The human-friendly name of the identity provider, used in the log in form.", |
| 24 | + }, |
| 25 | + "provider_id": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Default: "github", |
| 29 | + Description: "provider id, is always github, unless you have a extended custom implementation", |
| 30 | + }, |
| 31 | + "client_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + Description: "Client ID.", |
| 35 | + }, |
| 36 | + "client_secret": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + Sensitive: true, |
| 40 | + Description: "Client Secret.", |
| 41 | + }, |
| 42 | + "base_url": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + Default: "https://github.com", |
| 46 | + Description: "Base URL for the GitHub instance, defaults to https://github.com", |
| 47 | + }, |
| 48 | + "api_url": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + Default: "https://api.github.com", |
| 52 | + Description: "API URL for the GitHub instance, defaults to https://api.github.com", |
| 53 | + }, |
| 54 | + "github_json_format": { |
| 55 | + Type: schema.TypeBool, |
| 56 | + Optional: true, |
| 57 | + Default: false, |
| 58 | + Description: "Whether GitHub API shoulds accept JSON explicitly during token authentication requests, defaults to false", |
| 59 | + }, |
| 60 | + "default_scopes": { //defaultScope |
| 61 | + Type: schema.TypeString, |
| 62 | + Optional: true, |
| 63 | + Default: "user:email", |
| 64 | + Description: "The scopes to be sent when asking for authorization. See the documentation for possible values, separator and default value'. Default to 'user:email'", |
| 65 | + }, |
| 66 | + "hide_on_login_page": { |
| 67 | + Type: schema.TypeBool, |
| 68 | + Optional: true, |
| 69 | + Default: false, |
| 70 | + Description: "Hide On Login Page.", |
| 71 | + }, |
| 72 | + } |
| 73 | + oidcResource := resourceKeycloakIdentityProvider() |
| 74 | + oidcResource.Schema = mergeSchemas(oidcResource.Schema, oidcGithubSchema) |
| 75 | + oidcResource.CreateContext = resourceKeycloakIdentityProviderCreate(getOidcGithubIdentityProviderFromData, setOidcGithubIdentityProviderData) |
| 76 | + oidcResource.ReadContext = resourceKeycloakIdentityProviderRead(setOidcGithubIdentityProviderData) |
| 77 | + oidcResource.UpdateContext = resourceKeycloakIdentityProviderUpdate(getOidcGithubIdentityProviderFromData, setOidcGithubIdentityProviderData) |
| 78 | + return oidcResource |
| 79 | +} |
| 80 | + |
| 81 | +func getOidcGithubIdentityProviderFromData(data *schema.ResourceData, keycloakVersion *version.Version) (*keycloak.IdentityProvider, error) { |
| 82 | + rec, defaultConfig := getIdentityProviderFromData(data, keycloakVersion) |
| 83 | + rec.ProviderId = data.Get("provider_id").(string) |
| 84 | + |
| 85 | + aliasRaw, ok := data.GetOk("alias") |
| 86 | + if ok { |
| 87 | + rec.Alias = aliasRaw.(string) |
| 88 | + } else { |
| 89 | + rec.Alias = "github" |
| 90 | + } |
| 91 | + |
| 92 | + githubOidcIdentityProviderConfig := &keycloak.IdentityProviderConfig{ |
| 93 | + ClientId: data.Get("client_id").(string), |
| 94 | + ClientSecret: data.Get("client_secret").(string), |
| 95 | + DefaultScope: data.Get("default_scopes").(string), |
| 96 | + GithubJsonFormat: types.KeycloakBoolQuoted(data.Get("github_json_format").(bool)), |
| 97 | + BaseUrl: data.Get("base_url").(string), |
| 98 | + ApiUrl: data.Get("api_url").(string), |
| 99 | + |
| 100 | + //since keycloak v26 moved to IdentityProvider - still here fore backward compatibility |
| 101 | + HideOnLoginPage: types.KeycloakBoolQuoted(data.Get("hide_on_login_page").(bool)), |
| 102 | + } |
| 103 | + |
| 104 | + if err := mergo.Merge(githubOidcIdentityProviderConfig, defaultConfig); err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + rec.Config = githubOidcIdentityProviderConfig |
| 109 | + |
| 110 | + return rec, nil |
| 111 | +} |
| 112 | + |
| 113 | +func setOidcGithubIdentityProviderData(data *schema.ResourceData, identityProvider *keycloak.IdentityProvider, keycloakVersion *version.Version) error { |
| 114 | + setIdentityProviderData(data, identityProvider, keycloakVersion) |
| 115 | + data.Set("provider_id", identityProvider.ProviderId) |
| 116 | + data.Set("client_id", identityProvider.Config.ClientId) |
| 117 | + data.Set("github_json_format", identityProvider.Config.GithubJsonFormat) |
| 118 | + data.Set("base_url", identityProvider.Config.BaseUrl) |
| 119 | + data.Set("api_url", identityProvider.Config.ApiUrl) |
| 120 | + data.Set("default_scopes", identityProvider.Config.DefaultScope) |
| 121 | + |
| 122 | + if keycloakVersion.LessThan(keycloak.Version_26.AsVersion()) { |
| 123 | + // Since keycloak v26 the attribute "hideOnLoginPage" is not part of the identity provider config anymore! |
| 124 | + data.Set("hide_on_login_page", identityProvider.Config.HideOnLoginPage) |
| 125 | + return nil |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
0 commit comments