first working version

This commit is contained in:
ston1th 2022-07-10 15:51:31 +02:00
commit c9dfecc519
21 changed files with 296 additions and 178 deletions

View file

@ -4,7 +4,6 @@ package server
import (
"context"
"encoding/json"
"errors"
"html/template"
"net/http"
@ -87,7 +86,7 @@ type loginData struct {
type consentData struct {
ConsentChallenge string
Info *consentInfo
Info *client.ConsentRequest
}
func (c *Context) Exec() {
@ -302,155 +301,117 @@ func (c *Context) Admin() bool {
return c.Token.Claims.GetBool(adminClaim)
}
type respBody struct {
URL string `json:"redirect_to,omitempty"`
Error string `json:"error,omitempty"`
}
func (c *Context) ApproveLogin(user, challenge string) (rdr string, err error) {
req := c.Srv.API.AdminApi.AcceptLoginRequest(context.Background())
req.AcceptLoginRequest(*client.NewAcceptLoginRequest(user))
req.LoginChallenge(challenge)
_, resp, err := req.Execute()
cr, resp, err := c.Srv.API.AdminApi.AcceptLoginRequest(context.Background()).
AcceptLoginRequest(*client.NewAcceptLoginRequest(user)).
LoginChallenge(challenge).
Execute()
if err != nil {
return
}
defer resp.Body.Close()
rb := new(respBody)
err = json.NewDecoder(resp.Body).Decode(rb)
if err != nil {
return
}
if rb.Error != "" {
err = errors.New(rb.Error)
return
}
rdr = rb.URL
rdr = cr.RedirectTo
return
}
func (c *Context) ApproveConsent(challenge string) (rdr string, err error) {
req := c.Srv.API.AdminApi.AcceptConsentRequest(context.Background()).
AcceptConsentRequest(*client.NewAcceptConsentRequest()).
ConsentChallenge(challenge)
_, resp, err := req.Execute()
ci, err := c.Consent(challenge)
if err != nil {
return
}
accept := client.NewAcceptConsentRequest()
accept.GrantScope = ci.RequestedScope
accept.GrantAccessTokenAudience = ci.RequestedAccessTokenAudience
accept.Remember = new(bool)
*accept.Remember = true
cr, resp, err := c.Srv.API.AdminApi.AcceptConsentRequest(context.Background()).
AcceptConsentRequest(*accept).
ConsentChallenge(challenge).
Execute()
if err != nil {
return
}
defer resp.Body.Close()
rb := new(respBody)
err = json.NewDecoder(resp.Body).Decode(rb)
if err != nil {
return
}
if rb.Error != "" {
err = errors.New(rb.Error)
return
}
rdr = rb.URL
rdr = cr.RedirectTo
return
}
func (c *Context) RejectConsent(challenge string) (rdr string, err error) {
req := c.Srv.API.AdminApi.RejectConsentRequest(context.Background()).
cr, resp, err := c.Srv.API.AdminApi.RejectConsentRequest(context.Background()).
RejectRequest(*client.NewRejectRequest()).
ConsentChallenge(challenge)
_, resp, err := req.Execute()
ConsentChallenge(challenge).
Execute()
if err != nil {
return
}
defer resp.Body.Close()
rb := new(respBody)
err = json.NewDecoder(resp.Body).Decode(rb)
if err != nil {
return
}
if rb.Error != "" {
err = errors.New(rb.Error)
return
}
rdr = rb.URL
rdr = cr.RedirectTo
return
}
func (c *Context) Consent(challenge string) (ci *consentInfo, err error) {
req := c.Srv.API.AdminApi.GetConsentRequest(context.Background()).
ConsentChallenge(challenge)
_, resp, err := req.Execute()
func (c *Context) Consent(challenge string) (cr *client.ConsentRequest, err error) {
cr, resp, err := c.Srv.API.AdminApi.GetConsentRequest(context.Background()).
ConsentChallenge(challenge).
Execute()
if err != nil {
return
}
defer resp.Body.Close()
ci = new(consentInfo)
err = json.NewDecoder(resp.Body).Decode(ci)
return
}
type sess struct {
ID string
Time string
}
func (c *Context) Sessions(user string) (m map[string]sess, err error) {
ss, resp, err := c.Srv.API.AdminApi.ListSubjectConsentSessions(context.Background()).
Subject(user).
Execute()
if err != nil {
return
}
if ci.Error != "" {
err = errors.New(ci.Error)
defer resp.Body.Close()
m = make(map[string]sess)
for _, s := range ss {
name := s.ConsentRequest.Client.ClientName
if name == nil {
continue
}
n := *name
cid := s.ConsentRequest.Client.ClientId
if cid == nil {
continue
}
id := *cid
if _, ok := m[n]; !ok {
m[n] = sess{
id,
s.HandledAt.Format(timeFmt),
}
}
}
return
}
type consentInfo struct {
Acr string `json:"acr"`
Amr []string `json:"amr"`
Challenge string `json:"challenge"`
Error string `json:"error,omitempty"`
Client struct {
AllowedCorsOrigins []string `json:"allowed_cors_origins"`
Audience []string `json:"audience"`
BackchannelLogoutSessionRequired bool `json:"backchannel_logout_session_required"`
BackchannelLogoutURI string `json:"backchannel_logout_uri"`
ClientID string `json:"client_id"`
ClientName string `json:"client_name"`
ClientSecret string `json:"client_secret"`
ClientSecretExpiresAt int `json:"client_secret_expires_at"`
ClientURI string `json:"client_uri"`
Contacts []string `json:"contacts"`
CreatedAt time.Time `json:"created_at"`
FrontchannelLogoutSessionRequired bool `json:"frontchannel_logout_session_required"`
FrontchannelLogoutURI string `json:"frontchannel_logout_uri"`
GrantTypes []string `json:"grant_types"`
Jwks struct {
} `json:"jwks"`
JwksURI string `json:"jwks_uri"`
LogoURI string `json:"logo_uri"`
Metadata struct {
} `json:"metadata"`
Owner string `json:"owner"`
PolicyURI string `json:"policy_uri"`
PostLogoutRedirectUris []string `json:"post_logout_redirect_uris"`
RedirectUris []string `json:"redirect_uris"`
RegistrationAccessToken string `json:"registration_access_token"`
RegistrationClientURI string `json:"registration_client_uri"`
RequestObjectSigningAlg string `json:"request_object_signing_alg"`
RequestUris []string `json:"request_uris"`
ResponseTypes []string `json:"response_types"`
Scope string `json:"scope"`
SectorIdentifierURI string `json:"sector_identifier_uri"`
SubjectType string `json:"subject_type"`
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method"`
TokenEndpointAuthSigningAlg string `json:"token_endpoint_auth_signing_alg"`
TosURI string `json:"tos_uri"`
UpdatedAt time.Time `json:"updated_at"`
UserinfoSignedResponseAlg string `json:"userinfo_signed_response_alg"`
} `json:"client"`
LoginChallenge string `json:"login_challenge"`
LoginSessionID string `json:"login_session_id"`
OidcContext struct {
AcrValues []string `json:"acr_values"`
Display string `json:"display"`
IDTokenHintClaims struct {
} `json:"id_token_hint_claims"`
LoginHint string `json:"login_hint"`
UILocales []string `json:"ui_locales"`
} `json:"oidc_context"`
RequestURL string `json:"request_url"`
RequestedAccessTokenAudience []string `json:"requested_access_token_audience"`
RequestedScope []string `json:"requested_scope"`
Skip bool `json:"skip"`
Subject string `json:"subject"`
func (c *Context) RevokeSession(user, client string, all bool) (err error) {
if client == "" && !all {
return
}
req := c.Srv.API.AdminApi.RevokeConsentSessions(context.Background()).
Subject(user)
if all {
req = req.All(all)
} else {
req = req.Client(client)
}
resp, err := req.Execute()
if err != nil {
return
}
defer resp.Body.Close()
return
}
type ctxHandler func(*Context)