001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2012 Richard "Shred" Körber
005 *   http://flattr4j.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License / GNU Lesser
009 * General Public License as published by the Free Software Foundation,
010 * either version 3 of the License, or (at your option) any later version.
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License");
013 * you may not use this file except in compliance with the License.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
018 */
019package org.shredzone.flattr4j.oauth;
020
021import org.shredzone.flattr4j.exception.FlattrException;
022
023import android.content.Intent;
024import android.net.Uri;
025
026/**
027 * An extension of {@link FlattrAuthenticator} that helps through the authentication
028 * process on Android devices.
029 *
030 * @see <a href="http://www.shredzone.org/projects/flattr4j/wiki/OAuth_Android">How to
031 *      authenticate with flattr4j on Android</a>
032 * @author Richard "Shred" Körber
033 * @since 2.3
034 */
035public class AndroidAuthenticator extends FlattrAuthenticator {
036
037    /**
038     * Constructs a new instance with the given {@link ConsumerKey}.
039     *
040     * @param host
041     *            Host name that is configured in the manifest
042     * @param consumerKey
043     *            {@link ConsumerKey}
044     */
045    public AndroidAuthenticator(String host, ConsumerKey consumerKey) {
046        super(consumerKey);
047        setCallbackUrl("flattr4j://" + host + "/authenticate");
048    }
049
050    /**
051     * Constructs a new instance with the given consumer key and secret.
052     *
053     * @param host
054     *            Host name that is configured in the manifest
055     * @param key
056     *            Consumer key
057     * @param secret
058     *            Consumer secret
059     */
060    public AndroidAuthenticator(String host, String key, String secret) {
061        this(host, new ConsumerKey(key, secret));
062    }
063
064    /**
065     * Creates an {@link Intent} for forwarding the user to the Flattr web page for
066     * authentication.
067     * <p>
068     * When the returned activity is started, a browser is opened. It shows a Flattr web
069     * page asking the user to authenticate and grant the requested scopes for your
070     * application.
071     * <p>
072     * When the authentication completes successfully, your activity is resumed with your
073     * callback URL passed in.
074     *
075     * @return Created {@link Intent}
076     */
077    public Intent createAuthenticateIntent() throws FlattrException {
078        String url = super.authenticate();
079
080        Intent intent = new Intent(Intent.ACTION_VIEW);
081        intent.setData(Uri.parse(url));
082        return intent;
083    }
084
085    /**
086     * When the authentication was completed, your activity is resumed. The callback URL
087     * carrying the authentication code is passed in with the {@link Intent}.
088     *
089     * @param uri
090     *            {@link Uri} that was passed in with the the {@link Intent}, containing
091     *            the callback URL from Flattr. It is safe to pass {@code null} here.
092     * @return {@link AccessToken}, or {@code null} if the {@link Uri} did not provide a
093     *         valid code.
094     */
095    public AccessToken fetchAccessToken(Uri uri) throws FlattrException {
096        if (uri != null) {
097            String code = uri.getQueryParameter("code");
098            if (code != null) {
099                return fetchAccessToken(code);
100            }
101        }
102
103        return null;
104    }
105
106}