Debian Codegraph @

src/java/org/astrogrid/samp/web/WebCredentialPresenter.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7 import java.util.logging.Logger;
8 import org.astrogrid.samp.client.SampException;
9 import org.astrogrid.samp.httpd.HttpServer;
10
11 /**
12 * CredentialPresenter for use with the Web Profile.
13 *
14 * <p>Uses the following securityMap items:
15 * <dl>
16 * <dt>samp.name:</dt>
17 * <dd>Self-declared client name.
18 * Mandatory, but since it's supplied by the client, it doesn't tell
19 * you anything trustworthy.
20 * </dd>
21 * </dl>
22 * and the following HTTP headers:
23 * <dl>
24 * <dt>Origin:</dt>
25 * <dd>Application origin, present if CORS is in use.
26 * See <a href="http://www.w3.org/TR/cors/"
27 * >Cross-Origin Resource Sharing</a>,
28 * <a href="https://www.ietf.org/rfc/rfc6454.txt"
29 * >The Web Origin Concept (RFC 6454)</a>.
30 * </dd>
31 * <dt>Referer:</dt>
32 * <dd>Connection URL, present at whim of browser.
33 * See <a href="https://www.w3.org/Protocols/rfc2616/rfc2616.html"
34 * >HTTP/1.1 (RFC2616)</a>, sec 14.36.
35 * </dd>
36 * </dl>
37 *
38 * <p>The sole instance of this singleton class is {@link #INSTANCE}.
39 *
40 * @author Mark Taylor
41 * @since 20 Jun 2016
42 */
43 public class WebCredentialPresenter implements CredentialPresenter {
44
45 /** Singleton instance. */
46 public static final WebCredentialPresenter INSTANCE =
47 new WebCredentialPresenter();
48
49 /** Origin header. */
50 public static final String ORIGIN_HDR = "Origin";
51
52 /** Referer header. */
53 public static final String REFERER_HDR = "Referer";
54
55 private static final Logger logger_ =
56 Logger.getLogger( WebCredentialPresenter.class.getName() );
57
58 /**
59 * Private sole constructor prevents external instantiation.
60 */
61 private WebCredentialPresenter() {
62 }
63
64 public Presentation
65 createPresentation( HttpServer.Request request, Map securityMap,
66 AuthResourceBundle.Content authContent )
67 throws SampException {
68
69 // Application self-declared name. Required, but doesn't prove much.
70 String appName = ClientAuthorizers.getAppName( securityMap );
71
72 // Application origin (see http://www.w3.org/TR/cors/,
73 // http://tools.ietf.org/html/draft-abarth-origin);
74 // present if CORS is in use.
75 Map headerMap = request.getHeaderMap();
76 String origin = HttpServer.getHeader( headerMap, ORIGIN_HDR );
77
78 // Referer header (RFC2616 sec 14.36) - present at whim of browser.
79 String referer = HttpServer.getHeader( headerMap, REFERER_HDR );
80
81 final Map map = new LinkedHashMap();
82 map.put( authContent.nameWord(), appName );
83 map.put( authContent.originWord(), origin );
84 map.put( "URL", referer );
85
86 final Object[] lines;
87 if ( referer != null && origin != null &&
88 ! origin.equals( getOrigin( referer ) ) ) {
89 logger_.warning( "Origin/Referer header mismatch: "
90 + "\"" + origin + "\" != "
91 + "\"" + getOrigin( referer ) + "\"" );
92 lines = new Object[] {
93 "WARNING: Origin/Referer header mismatch!",
94 "WARNING: This looks suspicious.",
95 };
96 }
97 else {
98 lines = new Object[ 0 ];
99 }
100 return new Presentation() {
101 public Map getAuthEntries() {
102 return map;
103 }
104 public Object[] getAuthLines() {
105 return lines;
106 }
107 };
108 }
109
110 /**
111 * Returns the serialized origin for a given URI string.
112 * @see <a href="http://tools.ietf.org/html/draft-abarth-origin-09"
113 * >The Web Origin Concept</a>
114 *
115 * @param uri URI
116 * @return origin of <code>uri</code>,
117 * <code>null</code> (note: not "null") if it cannot be determined
118 */
119 private String getOrigin( String uri ) {
120 if ( uri == null ) {
121 return null;
122 }
123 URL url;
124 try {
125 url = new URL( uri );
126 }
127 catch ( MalformedURLException e ) {
128 return null;
129 }
130 String scheme = url.getProtocol();
131 String host = url.getHost();
132 int portnum = url.getPort();
133 StringBuffer sbuf = new StringBuffer()
134 .append( scheme )
135 .append( "://" )
136 .append( host );
137 if ( portnum >= 0 && portnum != url.getDefaultPort() ) {
138 sbuf.append( ":" )
139 .append( Integer.toString( portnum ) );
140 }
141 return sbuf.toString().toLowerCase();
142 }
143 }