2016-04-16 05:52:59 +05:30
|
|
|
package adminschema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/client"
|
|
|
|
"github.com/coreos/go-oidc/oidc"
|
|
|
|
)
|
|
|
|
|
2016-04-20 06:57:45 +05:30
|
|
|
var (
|
|
|
|
ErrorNoRedirectURI = errors.New("No Redirect URIs")
|
|
|
|
ErrorInvalidRedirectURI = errors.New("Invalid Redirect URI")
|
|
|
|
ErrorInvalidLogoURI = errors.New("Invalid Logo URI")
|
|
|
|
ErrorInvalidClientURI = errors.New("Invalid Client URI")
|
|
|
|
)
|
|
|
|
|
2016-04-16 05:52:59 +05:30
|
|
|
func MapSchemaClientToClient(sc Client) (client.Client, error) {
|
|
|
|
c := client.Client{
|
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: sc.Id,
|
|
|
|
Secret: sc.Secret,
|
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
|
|
|
RedirectURIs: make([]url.URL, len(sc.RedirectURIs)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, ru := range sc.RedirectURIs {
|
|
|
|
if ru == "" {
|
2016-04-20 06:57:45 +05:30
|
|
|
return client.Client{}, ErrorNoRedirectURI
|
2016-04-16 05:52:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(ru)
|
|
|
|
if err != nil {
|
2016-04-20 06:57:45 +05:30
|
|
|
return client.Client{}, ErrorInvalidRedirectURI
|
2016-04-16 05:52:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
c.Metadata.RedirectURIs[i] = *u
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Metadata.ClientName = sc.ClientName
|
|
|
|
|
|
|
|
if sc.LogoURI != "" {
|
|
|
|
logoURI, err := url.Parse(sc.LogoURI)
|
|
|
|
if err != nil {
|
2016-04-20 06:57:45 +05:30
|
|
|
return client.Client{}, ErrorInvalidLogoURI
|
2016-04-16 05:52:59 +05:30
|
|
|
}
|
|
|
|
c.Metadata.LogoURI = logoURI
|
|
|
|
}
|
|
|
|
|
|
|
|
if sc.ClientURI != "" {
|
|
|
|
clientURI, err := url.Parse(sc.ClientURI)
|
|
|
|
if err != nil {
|
2016-04-20 06:57:45 +05:30
|
|
|
return client.Client{}, ErrorInvalidClientURI
|
2016-04-16 05:52:59 +05:30
|
|
|
}
|
|
|
|
c.Metadata.ClientURI = clientURI
|
|
|
|
}
|
2016-04-20 06:57:45 +05:30
|
|
|
|
|
|
|
c.Admin = sc.IsAdmin
|
2016-04-16 05:52:59 +05:30
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MapClientToSchemaClient(c client.Client) Client {
|
|
|
|
cl := Client{
|
|
|
|
Id: c.Credentials.ID,
|
|
|
|
Secret: c.Credentials.Secret,
|
|
|
|
RedirectURIs: make([]string, len(c.Metadata.RedirectURIs)),
|
|
|
|
}
|
|
|
|
for i, u := range c.Metadata.RedirectURIs {
|
|
|
|
cl.RedirectURIs[i] = u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
cl.ClientName = c.Metadata.ClientName
|
|
|
|
|
|
|
|
if c.Metadata.LogoURI != nil {
|
|
|
|
cl.LogoURI = c.Metadata.LogoURI.String()
|
|
|
|
}
|
|
|
|
if c.Metadata.ClientURI != nil {
|
|
|
|
cl.ClientURI = c.Metadata.ClientURI.String()
|
|
|
|
}
|
2016-04-20 06:57:45 +05:30
|
|
|
cl.IsAdmin = c.Admin
|
2016-04-16 05:52:59 +05:30
|
|
|
return cl
|
|
|
|
}
|