Line data Source code
1 : // Copyright (C) 2010 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd.auth.openid; 16 : 17 : import static java.nio.charset.StandardCharsets.UTF_8; 18 : 19 : import com.google.gerrit.server.config.CanonicalWebUrl; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Provider; 22 : import com.google.inject.Singleton; 23 : import java.io.IOException; 24 : import javax.servlet.ServletOutputStream; 25 : import javax.servlet.http.HttpServlet; 26 : import javax.servlet.http.HttpServletRequest; 27 : import javax.servlet.http.HttpServletResponse; 28 : 29 : @Singleton 30 : class XrdsServlet extends HttpServlet { 31 : private static final long serialVersionUID = 1L; 32 : static final String LOCATION = "OpenID.XRDS"; 33 : 34 : private final Provider<String> url; 35 : 36 : @Inject 37 99 : XrdsServlet(@CanonicalWebUrl final Provider<String> url) { 38 99 : this.url = url; 39 99 : } 40 : 41 : @Override 42 : protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 43 0 : final StringBuilder r = new StringBuilder(); 44 0 : r.append("<?xml version=\"1.0\" encoding=\"").append(UTF_8.name()).append("\"?>"); 45 0 : r.append("<xrds:XRDS"); 46 0 : r.append(" xmlns:xrds=\"xri://$xrds\""); 47 0 : r.append(" xmlns:openid=\"http://openid.net/xmlns/1.0\""); 48 0 : r.append(" xmlns=\"xri://$xrd*($v*2.0)\">"); 49 0 : r.append("<XRD>"); 50 0 : r.append("<Service priority=\"1\">"); 51 0 : r.append("<Type>http://specs.openid.net/auth/2.0/return_to</Type>"); 52 0 : r.append("<URI>").append(url.get()).append(OpenIdServiceImpl.RETURN_URL).append("</URI>"); 53 0 : r.append("</Service>"); 54 0 : r.append("</XRD>"); 55 0 : r.append("</xrds:XRDS>"); 56 0 : r.append("\n"); 57 : 58 0 : final byte[] raw = r.toString().getBytes(UTF_8); 59 0 : rsp.setContentLength(raw.length); 60 0 : rsp.setContentType("application/xrds+xml"); 61 0 : rsp.setCharacterEncoding(UTF_8.name()); 62 : 63 0 : try (ServletOutputStream out = rsp.getOutputStream()) { 64 0 : out.write(raw); 65 : } 66 0 : } 67 : }