001package votorola.a.diff.harvest.auth;// Copyright 2011-2012. Christian Weilbach.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.io.IOException;
004
005import votorola.a.PagePropertyReader;
006import votorola.a.PageProperty;
007
008import votorola.a.WikiCache;
009import votorola.a.diff.harvest.cache.AuthDiffMessage;
010import votorola.a.diff.harvest.cache.DiffMessage;
011import votorola.a.diff.harvest.cache.HarvestCache;
012import votorola.a.voter.IDPair;
013import votorola.g.lang.ThreadSafe;
014
015/**
016 * Provide access to the semantic-mediawiki database for
017 * a single property on the user's page. This is a good default location
018 * for users to store their different network identities. 
019 * <a href="http://obsidian.reluk.ca/w/Property:IRC_nickname">IRC nickname</a> is
020 * an example.
021 *
022 * @see WikiCache
023 * @see HarvestCache
024 */
025public @ThreadSafe class WikiAuthenticator implements Authenticator {
026
027    /**
028     * Use the wiki cache to store already requested pages
029     * locally.
030     */
031    private final transient WikiCache wikiCache;
032
033    /**
034     * Semantic property to query on the user's page.
035     */
036    private final transient String property;
037
038    /**
039     * Construct this verifier.
040     * @param wc WikiCache to (re)use.
041     * @param property Property to query.
042     */
043    public WikiAuthenticator(final WikiCache wc, final String property) {
044        this.wikiCache = wc;
045        this.property = property;
046    }
047
048    /**
049     * Verify a message against this property.
050     * @param dmsg DiffMessage to verify.
051     * @return the message with authorship information set. Otherwise returns
052     * null.
053     * @throws IOException if accessing the WikiCache fails.
054     */
055    public final AuthDiffMessage verify(final DiffMessage dmsg) throws IOException
056    {
057        final String sender = dmsg.message().mc().sender();
058        final String aMail = dmsg.draftPair().aCore().person().email();
059        final String bMail = dmsg.draftPair().bCore().person().email();
060        final PagePropertyReader aPropReader 
061            = new PagePropertyReader( wikiCache, "User:" + IDPair.toUsername(aMail)
062                    , new PageProperty(property));
063        final PagePropertyReader bPropReader 
064            = new PagePropertyReader( wikiCache, "User:" + IDPair.toUsername(bMail)
065                    , new PageProperty(property));
066        final String aProp = aPropReader.hasNext() ? aPropReader.read().getValue() : null ;
067        final String bProp = bPropReader.hasNext() ? bPropReader.read().getValue() : null ;
068        if(sender.equals(aProp)) {
069            return AuthDiffMessage.create(dmsg, aMail, bMail, "a");
070        }
071        if(sender.equals(bProp)) {
072            return AuthDiffMessage.create(dmsg, bMail, aMail, "b");
073        }
074
075        return null;
076    }
077
078}