1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.internal

File XWikiSecretVerifier.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

4
14
2
1
91
39
5
0.36
7
2
2.5

Classes

Class Line # Actions
XWikiSecretVerifier 39 14 0% 5 17
0.1515%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rest.internal;
21   
22    import java.security.Principal;
23    import java.util.logging.Level;
24   
25    import org.restlet.Context;
26    import org.restlet.security.SecretVerifier;
27    import org.xwiki.component.manager.ComponentManager;
28   
29    import com.xpn.xwiki.XWiki;
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.XWikiException;
32   
33    /**
34    * Secret verifier that authenticates the user against XWiki authentication system.
35    *
36    * @version $Id: 9e9905d6cb1e1bacc8cd59e00d143f5d2fa6ea56 $
37    * @since 3.2M1
38    */
 
39    public class XWikiSecretVerifier extends SecretVerifier
40    {
41   
42    /**
43    * XWiki component manager. Used to access legacy XWiki context.
44    */
45    private ComponentManager componentManager;
46   
47    /**
48    * Restlet context, used to access Restlet logger.
49    */
50    private Context context;
51   
52    /**
53    * Constructor.
54    *
55    * @param context the Restlet context in which to verify the secrect
56    * @param manager XWiki's component manager
57    */
 
58  18 toggle public XWikiSecretVerifier(Context context, ComponentManager manager)
59    {
60  18 this.context = context;
61  18 this.componentManager = manager;
62    }
63   
 
64  0 toggle @Override
65    public boolean verify(String identifier, char[] secret) throws IllegalArgumentException
66    {
67  0 XWikiContext xwikiContext = Utils.getXWikiContext(this.componentManager);
68  0 XWiki xwiki = Utils.getXWiki(this.componentManager);
69   
70  0 try {
71  0 Principal principal = (secret == null) ? null : xwiki.getAuthService().authenticate(identifier,
72    new String(secret), xwikiContext);
73  0 if (principal != null) {
74  0 String xwikiUser = principal.getName();
75   
76  0 xwikiContext.setUser(xwikiUser);
77   
78  0 this.context.getLogger().log(Level.FINE, String.format("Authenticated as '%s'.", identifier));
79   
80  0 return true;
81    }
82    } catch (XWikiException e) {
83  0 this.context.getLogger().log(Level.WARNING, "Exception occurred while authenticating.", e);
84    }
85   
86  0 this.context.getLogger().log(Level.WARNING, String.format("Cannot authenticate '%s'.", identifier));
87   
88  0 return false;
89    }
90   
91    }