1
18
19 package io.undertow.security.impl;
20
21 import io.undertow.security.api.AuthenticationMechanism;
22 import io.undertow.security.api.AuthenticationMechanismFactory;
23 import io.undertow.security.api.SecurityContext;
24 import io.undertow.security.idm.Account;
25 import io.undertow.security.idm.ExternalCredential;
26 import io.undertow.security.idm.IdentityManager;
27 import io.undertow.server.HttpServerExchange;
28 import io.undertow.server.handlers.form.FormParserFactory;
29 import io.undertow.util.AttachmentKey;
30
31 import java.util.Map;
32
33
43 public class ExternalAuthenticationMechanism implements AuthenticationMechanism {
44
45 public static final AuthenticationMechanismFactory FACTORY = new Factory();
46
47 public static final String NAME = "EXTERNAL";
48
49 private final String name;
50 private final IdentityManager identityManager;
51
52 public static final AttachmentKey<String> EXTERNAL_PRINCIPAL = AttachmentKey.create(String.class);
53 public static final AttachmentKey<String> EXTERNAL_AUTHENTICATION_TYPE = AttachmentKey.create(String.class);
54
55 public ExternalAuthenticationMechanism(String name, IdentityManager identityManager) {
56 this.name = name;
57 this.identityManager = identityManager;
58 }
59
60 public ExternalAuthenticationMechanism(String name) {
61 this(name, null);
62 }
63 public ExternalAuthenticationMechanism() {
64 this(NAME);
65 }
66
67 @SuppressWarnings("deprecation")
68 private IdentityManager getIdentityManager(SecurityContext securityContext) {
69 return identityManager != null ? identityManager : securityContext.getIdentityManager();
70 }
71
72 @Override
73 public AuthenticationMechanismOutcome authenticate(HttpServerExchange exchange, SecurityContext securityContext) {
74 String principal = exchange.getAttachment(EXTERNAL_PRINCIPAL);
75 if(principal == null) {
76 return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
77 }
78 Account account = getIdentityManager(securityContext).verify(principal, ExternalCredential.INSTANCE);
79 if(account == null) {
80 return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
81 }
82 String name = exchange.getAttachment(EXTERNAL_AUTHENTICATION_TYPE);
83 securityContext.authenticationComplete(account, name != null ? name: this.name, false);
84
85 return AuthenticationMechanismOutcome.AUTHENTICATED;
86 }
87
88 @Override
89 public ChallengeResult sendChallenge(HttpServerExchange exchange, SecurityContext securityContext) {
90 return ChallengeResult.NOT_SENT;
91 }
92
93 public static final class Factory implements AuthenticationMechanismFactory {
94
95 @Deprecated
96 public Factory(IdentityManager identityManager) {}
97
98 public Factory() {}
99
100 @Override
101 public AuthenticationMechanism create(String mechanismName,IdentityManager identityManager, FormParserFactory formParserFactory, Map<String, String> properties) {
102 return new ExternalAuthenticationMechanism(mechanismName, identityManager);
103 }
104 }
105 }
106