keycloak-service
公開メンバ関数 | 限定公開メンバ関数 | 静的限定公開変数類 | 非公開メンバ関数 | 静的非公開メンバ関数 | 非公開変数類 | 全メンバ一覧
org.keycloak.broker.saml.SAMLIdentityProvider クラス
org.keycloak.broker.saml.SAMLIdentityProvider の継承関係図
Inheritance graph
org.keycloak.broker.saml.SAMLIdentityProvider 連携図
Collaboration graph

公開メンバ関数

 SAMLIdentityProvider (KeycloakSession session, SAMLIdentityProviderConfig config, DestinationValidator destinationValidator)
 
Object callback (RealmModel realm, AuthenticationCallback callback, EventBuilder event)
 
Response performLogin (AuthenticationRequest request)
 
void authenticationFinished (AuthenticationSessionModel authSession, BrokeredIdentityContext context)
 
Response retrieveToken (KeycloakSession session, FederatedIdentityModel identity)
 
void backchannelLogout (KeycloakSession session, UserSessionModel userSession, UriInfo uriInfo, RealmModel realm)
 
Response keycloakInitiatedBrowserLogout (KeycloakSession session, UserSessionModel userSession, UriInfo uriInfo, RealmModel realm)
 
Response export (UriInfo uriInfo, RealmModel realm, String format)
 
SignatureAlgorithm getSignatureAlgorithm ()
 
IdentityProviderDataMarshaller getMarshaller ()
 

限定公開メンバ関数

SAML2LogoutRequestBuilder buildLogoutRequest (UserSessionModel userSession, UriInfo uriInfo, RealmModel realm, String singleLogoutServiceUrl)
 

静的限定公開変数類

static final Logger logger = Logger.getLogger(SAMLIdentityProvider.class)
 

非公開メンバ関数

String getEntityId (UriInfo uriInfo, RealmModel realm)
 
JaxrsSAML2BindingBuilder buildLogoutBinding (KeycloakSession session, UserSessionModel userSession, RealmModel realm)
 

静的非公開メンバ関数

static void addKeyInfo (StringBuilder target, RsaKeyMetadata key, String purpose)
 

非公開変数類

final DestinationValidator destinationValidator
 

詳解

著者
Pedro Igor

構築子と解体子

◆ SAMLIdentityProvider()

org.keycloak.broker.saml.SAMLIdentityProvider.SAMLIdentityProvider ( KeycloakSession  session,
SAMLIdentityProviderConfig  config,
DestinationValidator  destinationValidator 
)
inline
55  {
56  super(session, config);
58  }
final DestinationValidator destinationValidator
Definition: SAMLIdentityProvider.java:54

関数詳解

◆ addKeyInfo()

static void org.keycloak.broker.saml.SAMLIdentityProvider.addKeyInfo ( StringBuilder  target,
RsaKeyMetadata  key,
String  purpose 
)
inlinestaticprivate
255  {
256  if (key == null) {
257  return;
258  }
259 
260  target.append(SPMetadataDescriptor.xmlKeyInfo(" ", key.getKid(), PemUtils.encodeCertificate(key.getCertificate()), purpose, true));
261  }

◆ authenticationFinished()

void org.keycloak.broker.saml.SAMLIdentityProvider.authenticationFinished ( AuthenticationSessionModel  authSession,
BrokeredIdentityContext  context 
)
inline
126  {
127  ResponseType responseType = (ResponseType)context.getContextData().get(SAMLEndpoint.SAML_LOGIN_RESPONSE);
128  AssertionType assertion = (AssertionType)context.getContextData().get(SAMLEndpoint.SAML_ASSERTION);
129  SubjectType subject = assertion.getSubject();
130  SubjectType.STSubType subType = subject.getSubType();
131  NameIDType subjectNameID = (NameIDType) subType.getBaseID();
132  authSession.setUserSessionNote(SAMLEndpoint.SAML_FEDERATED_SUBJECT, subjectNameID.getValue());
133  if (subjectNameID.getFormat() != null) authSession.setUserSessionNote(SAMLEndpoint.SAML_FEDERATED_SUBJECT_NAMEFORMAT, subjectNameID.getFormat().toString());
134  AuthnStatementType authn = (AuthnStatementType)context.getContextData().get(SAMLEndpoint.SAML_AUTHN_STATEMENT);
135  if (authn != null && authn.getSessionIndex() != null) {
136  authSession.setUserSessionNote(SAMLEndpoint.SAML_FEDERATED_SESSION_INDEX, authn.getSessionIndex());
137 
138  }
139  }

◆ backchannelLogout()

void org.keycloak.broker.saml.SAMLIdentityProvider.backchannelLogout ( KeycloakSession  session,
UserSessionModel  userSession,
UriInfo  uriInfo,
RealmModel  realm 
)
inline
147  {
148  String singleLogoutServiceUrl = getConfig().getSingleLogoutServiceUrl();
149  if (singleLogoutServiceUrl == null || singleLogoutServiceUrl.trim().equals("") || !getConfig().isBackchannelSupported()) return;
150  SAML2LogoutRequestBuilder logoutBuilder = buildLogoutRequest(userSession, uriInfo, realm, singleLogoutServiceUrl);
151  JaxrsSAML2BindingBuilder binding = buildLogoutBinding(session, userSession, realm);
152  try {
153  int status = SimpleHttp.doPost(singleLogoutServiceUrl, session)
154  .param(GeneralConstants.SAML_REQUEST_KEY, binding.postBinding(logoutBuilder.buildDocument()).encoded())
155  .param(GeneralConstants.RELAY_STATE, userSession.getId()).asStatus();
156  boolean success = status >=200 && status < 400;
157  if (!success) {
158  logger.warn("Failed saml backchannel broker logout to: " + singleLogoutServiceUrl);
159  }
160  } catch (Exception e) {
161  logger.warn("Failed saml backchannel broker logout to: " + singleLogoutServiceUrl, e);
162  }
163 
164  }
JaxrsSAML2BindingBuilder buildLogoutBinding(KeycloakSession session, UserSessionModel userSession, RealmModel realm)
Definition: SAMLIdentityProvider.java:199
static final Logger logger
Definition: SAMLIdentityProvider.java:53
SAML2LogoutRequestBuilder buildLogoutRequest(UserSessionModel userSession, UriInfo uriInfo, RealmModel realm, String singleLogoutServiceUrl)
Definition: SAMLIdentityProvider.java:189

◆ buildLogoutBinding()

JaxrsSAML2BindingBuilder org.keycloak.broker.saml.SAMLIdentityProvider.buildLogoutBinding ( KeycloakSession  session,
UserSessionModel  userSession,
RealmModel  realm 
)
inlineprivate
199  {
200  JaxrsSAML2BindingBuilder binding = new JaxrsSAML2BindingBuilder()
201  .relayState(userSession.getId());
202  if (getConfig().isWantAuthnRequestsSigned()) {
203  KeyManager.ActiveRsaKey keys = session.keys().getActiveRsaKey(realm);
204  String keyName = getConfig().getXmlSigKeyInfoKeyNameTransformer().getKeyName(keys.getKid(), keys.getCertificate());
205  binding.signWith(keyName, keys.getPrivateKey(), keys.getPublicKey(), keys.getCertificate())
206  .signatureAlgorithm(getSignatureAlgorithm())
207  .signDocument();
208  }
209  return binding;
210  }
SignatureAlgorithm getSignatureAlgorithm()
Definition: SAMLIdentityProvider.java:263

◆ buildLogoutRequest()

SAML2LogoutRequestBuilder org.keycloak.broker.saml.SAMLIdentityProvider.buildLogoutRequest ( UserSessionModel  userSession,
UriInfo  uriInfo,
RealmModel  realm,
String  singleLogoutServiceUrl 
)
inlineprotected
189  {
190  SAML2LogoutRequestBuilder logoutBuilder = new SAML2LogoutRequestBuilder()
191  .assertionExpiration(realm.getAccessCodeLifespan())
192  .issuer(getEntityId(uriInfo, realm))
193  .sessionIndex(userSession.getNote(SAMLEndpoint.SAML_FEDERATED_SESSION_INDEX))
194  .userPrincipal(userSession.getNote(SAMLEndpoint.SAML_FEDERATED_SUBJECT), userSession.getNote(SAMLEndpoint.SAML_FEDERATED_SUBJECT_NAMEFORMAT))
195  .destination(singleLogoutServiceUrl);
196  return logoutBuilder;
197  }
String getEntityId(UriInfo uriInfo, RealmModel realm)
Definition: SAMLIdentityProvider.java:121

◆ callback()

Object org.keycloak.broker.saml.SAMLIdentityProvider.callback ( RealmModel  realm,
AuthenticationCallback  callback,
EventBuilder  event 
)
inline
61  {
62  return new SAMLEndpoint(realm, this, getConfig(), callback, destinationValidator);
63  }
final DestinationValidator destinationValidator
Definition: SAMLIdentityProvider.java:54
Object callback(RealmModel realm, AuthenticationCallback callback, EventBuilder event)
Definition: SAMLIdentityProvider.java:61

◆ export()

Response org.keycloak.broker.saml.SAMLIdentityProvider.export ( UriInfo  uriInfo,
RealmModel  realm,
String  format 
)
inline
213  {
214 
215  String authnBinding = JBossSAMLURIConstants.SAML_HTTP_REDIRECT_BINDING.get();
216 
217  if (getConfig().isPostBindingAuthnRequest()) {
218  authnBinding = JBossSAMLURIConstants.SAML_HTTP_POST_BINDING.get();
219  }
220 
221  String endpoint = uriInfo.getBaseUriBuilder()
222  .path("realms").path(realm.getName())
223  .path("broker")
224  .path(getConfig().getAlias())
225  .path("endpoint")
226  .build().toString();
227 
228 
229  boolean wantAuthnRequestsSigned = getConfig().isWantAuthnRequestsSigned();
230  boolean wantAssertionsSigned = getConfig().isWantAssertionsSigned();
231  boolean wantAssertionsEncrypted = getConfig().isWantAssertionsEncrypted();
232  String entityId = getEntityId(uriInfo, realm);
233  String nameIDPolicyFormat = getConfig().getNameIDPolicyFormat();
234 
235  StringBuilder signingKeysString = new StringBuilder();
236  StringBuilder encryptionKeysString = new StringBuilder();
237  Set<RsaKeyMetadata> keys = new TreeSet<>((o1, o2) -> o1.getStatus() == o2.getStatus() // Status can be only PASSIVE OR ACTIVE, push PASSIVE to end of list
238  ? (int) (o2.getProviderPriority() - o1.getProviderPriority())
239  : (o1.getStatus() == KeyStatus.PASSIVE ? 1 : -1));
240  keys.addAll(session.keys().getRsaKeys(realm));
241  for (RsaKeyMetadata key : keys) {
242  addKeyInfo(signingKeysString, key, KeyTypes.SIGNING.value());
243 
244  if (key.getStatus() == KeyStatus.ACTIVE) {
245  addKeyInfo(encryptionKeysString, key, KeyTypes.ENCRYPTION.value());
246  }
247  }
248  String descriptor = SPMetadataDescriptor.getSPDescriptor(authnBinding, endpoint, endpoint,
249  wantAuthnRequestsSigned, wantAssertionsSigned, wantAssertionsEncrypted,
250  entityId, nameIDPolicyFormat, signingKeysString.toString(), encryptionKeysString.toString());
251 
252  return Response.ok(descriptor, MediaType.APPLICATION_XML_TYPE).build();
253  }
String getEntityId(UriInfo uriInfo, RealmModel realm)
Definition: SAMLIdentityProvider.java:121
static void addKeyInfo(StringBuilder target, RsaKeyMetadata key, String purpose)
Definition: SAMLIdentityProvider.java:255

◆ getEntityId()

String org.keycloak.broker.saml.SAMLIdentityProvider.getEntityId ( UriInfo  uriInfo,
RealmModel  realm 
)
inlineprivate
121  {
122  return UriBuilder.fromUri(uriInfo.getBaseUri()).path("realms").path(realm.getName()).build().toString();
123  }

◆ getMarshaller()

IdentityProviderDataMarshaller org.keycloak.broker.saml.SAMLIdentityProvider.getMarshaller ( )
inline
273  {
274  return new SAMLDataMarshaller();
275  }

◆ getSignatureAlgorithm()

SignatureAlgorithm org.keycloak.broker.saml.SAMLIdentityProvider.getSignatureAlgorithm ( )
inline
263  {
264  String alg = getConfig().getSignatureAlgorithm();
265  if (alg != null) {
266  SignatureAlgorithm algorithm = SignatureAlgorithm.valueOf(alg);
267  if (algorithm != null) return algorithm;
268  }
269  return SignatureAlgorithm.RSA_SHA256;
270  }

◆ keycloakInitiatedBrowserLogout()

Response org.keycloak.broker.saml.SAMLIdentityProvider.keycloakInitiatedBrowserLogout ( KeycloakSession  session,
UserSessionModel  userSession,
UriInfo  uriInfo,
RealmModel  realm 
)
inline
167  {
168  String singleLogoutServiceUrl = getConfig().getSingleLogoutServiceUrl();
169  if (singleLogoutServiceUrl == null || singleLogoutServiceUrl.trim().equals("")) return null;
170 
171  if (getConfig().isBackchannelSupported()) {
172  backchannelLogout(session, userSession, uriInfo, realm);
173  return null;
174  } else {
175  try {
176  SAML2LogoutRequestBuilder logoutBuilder = buildLogoutRequest(userSession, uriInfo, realm, singleLogoutServiceUrl);
177  JaxrsSAML2BindingBuilder binding = buildLogoutBinding(session, userSession, realm);
178  if (getConfig().isPostBindingLogout()) {
179  return binding.postBinding(logoutBuilder.buildDocument()).request(singleLogoutServiceUrl);
180  } else {
181  return binding.redirectBinding(logoutBuilder.buildDocument()).request(singleLogoutServiceUrl);
182  }
183  } catch (Exception e) {
184  throw new RuntimeException(e);
185  }
186  }
187  }
JaxrsSAML2BindingBuilder buildLogoutBinding(KeycloakSession session, UserSessionModel userSession, RealmModel realm)
Definition: SAMLIdentityProvider.java:199
void backchannelLogout(KeycloakSession session, UserSessionModel userSession, UriInfo uriInfo, RealmModel realm)
Definition: SAMLIdentityProvider.java:147
SAML2LogoutRequestBuilder buildLogoutRequest(UserSessionModel userSession, UriInfo uriInfo, RealmModel realm, String singleLogoutServiceUrl)
Definition: SAMLIdentityProvider.java:189

◆ performLogin()

Response org.keycloak.broker.saml.SAMLIdentityProvider.performLogin ( AuthenticationRequest  request)
inline
66  {
67  try {
68  UriInfo uriInfo = request.getUriInfo();
69  RealmModel realm = request.getRealm();
70  String issuerURL = getEntityId(uriInfo, realm);
71  String destinationUrl = getConfig().getSingleSignOnServiceUrl();
72  String nameIDPolicyFormat = getConfig().getNameIDPolicyFormat();
73 
74  if (nameIDPolicyFormat == null) {
75  nameIDPolicyFormat = JBossSAMLURIConstants.NAMEID_FORMAT_PERSISTENT.get();
76  }
77 
78  String protocolBinding = JBossSAMLURIConstants.SAML_HTTP_REDIRECT_BINDING.get();
79 
80  String assertionConsumerServiceUrl = request.getRedirectUri();
81 
82  if (getConfig().isPostBindingResponse()) {
83  protocolBinding = JBossSAMLURIConstants.SAML_HTTP_POST_BINDING.get();
84  }
85 
86  SAML2AuthnRequestBuilder authnRequestBuilder = new SAML2AuthnRequestBuilder()
87  .assertionConsumerUrl(assertionConsumerServiceUrl)
88  .destination(destinationUrl)
89  .issuer(issuerURL)
90  .forceAuthn(getConfig().isForceAuthn())
91  .protocolBinding(protocolBinding)
92  .nameIdPolicy(SAML2NameIDPolicyBuilder.format(nameIDPolicyFormat));
93  JaxrsSAML2BindingBuilder binding = new JaxrsSAML2BindingBuilder()
94  .relayState(request.getState().getEncoded());
95  boolean postBinding = getConfig().isPostBindingAuthnRequest();
96 
97  if (getConfig().isWantAuthnRequestsSigned()) {
98  KeyManager.ActiveRsaKey keys = session.keys().getActiveRsaKey(realm);
99 
100  KeyPair keypair = new KeyPair(keys.getPublicKey(), keys.getPrivateKey());
101 
102  String keyName = getConfig().getXmlSigKeyInfoKeyNameTransformer().getKeyName(keys.getKid(), keys.getCertificate());
103  binding.signWith(keyName, keypair);
104  binding.signatureAlgorithm(getSignatureAlgorithm());
105  binding.signDocument();
106  if (! postBinding && getConfig().isAddExtensionsElementWithKeyInfo()) { // Only include extension if REDIRECT binding and signing whole SAML protocol message
107  authnRequestBuilder.addExtension(new KeycloakKeySamlExtensionGenerator(keyName));
108  }
109  }
110 
111  if (postBinding) {
112  return binding.postBinding(authnRequestBuilder.toDocument()).request(destinationUrl);
113  } else {
114  return binding.redirectBinding(authnRequestBuilder.toDocument()).request(destinationUrl);
115  }
116  } catch (Exception e) {
117  throw new IdentityBrokerException("Could not create authentication request.", e);
118  }
119  }
String getEntityId(UriInfo uriInfo, RealmModel realm)
Definition: SAMLIdentityProvider.java:121
SignatureAlgorithm getSignatureAlgorithm()
Definition: SAMLIdentityProvider.java:263

◆ retrieveToken()

Response org.keycloak.broker.saml.SAMLIdentityProvider.retrieveToken ( KeycloakSession  session,
FederatedIdentityModel  identity 
)
inline
142  {
143  return Response.ok(identity.getToken()).build();
144  }

メンバ詳解

◆ destinationValidator

final DestinationValidator org.keycloak.broker.saml.SAMLIdentityProvider.destinationValidator
private

◆ logger

final Logger org.keycloak.broker.saml.SAMLIdentityProvider.logger = Logger.getLogger(SAMLIdentityProvider.class)
staticprotected

このクラス詳解は次のファイルから抽出されました: