forked from pgjdbc/pgjdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeGSS.java
77 lines (61 loc) · 2.52 KB
/
MakeGSS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (c) 2008, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.gss;
import static org.postgresql.util.internal.Nullness.castNonNull;
import org.postgresql.PGProperty;
import org.postgresql.core.PGStream;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.IOException;
import java.security.PrivilegedAction;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
public class MakeGSS {
private static final Logger LOGGER = Logger.getLogger(MakeGSS.class.getName());
public static void authenticate(boolean encrypted,
PGStream pgStream, String host, String user, char @Nullable [] password,
@Nullable String jaasApplicationName, @Nullable String kerberosServerName,
boolean useSpnego, boolean jaasLogin,
boolean logServerErrorDetail)
throws IOException, PSQLException {
LOGGER.log(Level.FINEST, " <=BE AuthenticationReqGSS");
if (jaasApplicationName == null) {
jaasApplicationName = PGProperty.JAAS_APPLICATION_NAME.getDefaultValue();
}
if (kerberosServerName == null) {
kerberosServerName = "postgres";
}
@Nullable Exception result;
try {
boolean performAuthentication = jaasLogin;
LoginContext lc = new LoginContext(castNonNull(jaasApplicationName), new GSSCallbackHandler(user, password));
lc.login();
Subject sub = lc.getSubject();
if ( encrypted ) {
PrivilegedAction<@Nullable Exception> action = new GssEncAction(pgStream, sub, host, user,
kerberosServerName, useSpnego, logServerErrorDetail);
result = Subject.doAs(sub, action);
} else {
PrivilegedAction<@Nullable Exception> action = new GssAction(pgStream, sub, host,
kerberosServerName, useSpnego, logServerErrorDetail);
result = Subject.doAs(sub, action);
}
} catch (Exception e) {
throw new PSQLException(GT.tr("GSS Authentication failed"), PSQLState.CONNECTION_FAILURE, e);
}
if (result instanceof IOException) {
throw (IOException) result;
} else if (result instanceof PSQLException) {
throw (PSQLException) result;
} else if (result != null) {
throw new PSQLException(GT.tr("GSS Authentication failed"), PSQLState.CONNECTION_FAILURE,
result);
}
}
}