1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rest.internal; |
21 |
|
|
22 |
|
import java.util.logging.Level; |
23 |
|
|
24 |
|
import org.restlet.Context; |
25 |
|
import org.restlet.Request; |
26 |
|
import org.restlet.Response; |
27 |
|
import org.restlet.data.ChallengeScheme; |
28 |
|
import org.restlet.data.Form; |
29 |
|
import org.restlet.engine.http.header.HeaderConstants; |
30 |
|
import org.restlet.security.ChallengeAuthenticator; |
31 |
|
import org.xwiki.component.manager.ComponentLookupException; |
32 |
|
import org.xwiki.component.manager.ComponentManager; |
33 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
34 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
35 |
|
import org.xwiki.rest.internal.resources.BrowserAuthenticationResource; |
36 |
|
|
37 |
|
import com.xpn.xwiki.XWiki; |
38 |
|
import com.xpn.xwiki.XWikiContext; |
39 |
|
import com.xpn.xwiki.XWikiException; |
40 |
|
import com.xpn.xwiki.user.api.XWikiUser; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@version |
61 |
|
|
|
|
| 86.1% |
Uncovered Elements: 5 (36) |
Complexity: 7 |
Complexity Density: 0.25 |
|
62 |
|
public class XWikiAuthentication extends ChallengeAuthenticator |
63 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
18 |
public XWikiAuthentication(Context context) throws IllegalArgumentException... |
65 |
|
{ |
66 |
18 |
super(context, true, ChallengeScheme.CUSTOM, "XWiki"); |
67 |
|
} |
68 |
|
|
|
|
| 84.8% |
Uncovered Elements: 5 (33) |
Complexity: 6 |
Complexity Density: 0.22 |
|
69 |
1709 |
@Override... |
70 |
|
public boolean authenticate(Request request, Response response) |
71 |
|
{ |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
1709 |
if (request.getResourceRef().getPath().endsWith(BrowserAuthenticationResource.URI_PATTERN)) { |
77 |
0 |
return super.authenticate(request, response); |
78 |
|
} |
79 |
|
|
80 |
1709 |
ComponentManager componentManager = |
81 |
|
(ComponentManager) getContext().getAttributes().get(Constants.XWIKI_COMPONENT_MANAGER); |
82 |
1709 |
XWikiContext xwikiContext = Utils.getXWikiContext(componentManager); |
83 |
1709 |
XWiki xwiki = Utils.getXWiki(componentManager); |
84 |
|
|
85 |
1709 |
DocumentReferenceResolver<String> resolver; |
86 |
1709 |
EntityReferenceSerializer<String> serializer; |
87 |
1709 |
try { |
88 |
1709 |
resolver = componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING, "current"); |
89 |
1709 |
serializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING); |
90 |
|
} catch (ComponentLookupException e1) { |
91 |
0 |
return false; |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
1709 |
xwikiContext.setUserReference(null); |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
1709 |
Form responseHeaders = (Form) response.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS); |
102 |
1709 |
if (responseHeaders == null) { |
103 |
1709 |
responseHeaders = new Form(); |
104 |
1709 |
response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders); |
105 |
|
} |
106 |
1709 |
responseHeaders.add("XWiki-User", serializer.serialize(xwikiContext.getUserReference())); |
107 |
1709 |
responseHeaders.add("XWiki-Version", xwikiContext.getWiki().getVersion()); |
108 |
|
|
109 |
|
|
110 |
1709 |
try { |
111 |
1709 |
XWikiUser xwikiUser = xwiki.checkAuth(xwikiContext); |
112 |
1709 |
if (xwikiUser != null) { |
113 |
|
|
114 |
770 |
xwikiContext.setUserReference(resolver.resolve(xwikiUser.getUser())); |
115 |
|
|
116 |
770 |
getLogger().fine(String.format("Authenticated as '%s'.", xwikiUser.getUser())); |
117 |
|
|
118 |
|
|
119 |
770 |
responseHeaders.set("XWiki-User", serializer.serialize(xwikiContext.getUserReference())); |
120 |
|
|
121 |
770 |
return true; |
122 |
|
} |
123 |
|
} catch (XWikiException e) { |
124 |
0 |
getLogger().log(Level.WARNING, "Exception occurred while authenticating.", e); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
939 |
return super.authenticate(request, response); |
129 |
|
} |
130 |
|
|
131 |
|
} |