<commit_before># The simple way of calling authenticated requests in retrofit style
[![Build Status](https://travis-ci.org/Unic8/retroauth.svg?branch=master)](https://travis-ci.org/Unic8/retroauth)
## Dependencies
* [Retrofit](https://github.com/square/retrofit) 1.9.0
* [RxJava](https://github.com/ReactiveX/RxJava) 1.0.14
* appcompat-v7: 23.0.0

Min SDK Version: 9

## Example:
Your services using retrofit:
``` java
public interface SomeService {
    @GET("/some/path")
    Observable<ResultObject> someRxJavaCall();
}
```
Your services using retroauth:
``` java
@Authentication(accountType = R.string.auth_account_type, tokenType = R.string.auth_token_type)
public interface SomeService {
    @Authenticated
    @GET("/some/path")
    Observable<ResultObject> someAuthenticatedRxJavaCall();
}

```
## What does it do?
If you call a request method, annotated with the authenticated annotation, it'll do the following steps:
* Step 1: Checks if there already is an account in the Android AccountManager. If not, it'll open a LoginActivity (you choose which). If there already is an account, go on with step 2, If there's more than one account open an Dialog to pick an account.
* Step 2: Tries to get the authentication token from the (choosen) account to add it to the request header. If there is no valid token, your LoginActivity could open with the pre-filled accounts username. After login go to Step 1.
* Step 3: Sends the actual request
* Step 4: If the request fails with an 401 (by default, but changeable) it invalidates the used token in the Android AccountManager and continues with step 1.

Sequence Diagrams can be found in the DIAGRAMS.md file

## How to use it?
Add it as dependency:
```groovy
compile 'eu.unicate.android:retroauth:1.0.3'
```

### 1. Create 3 strings in your strings.xml
i.e.
``` xml
<resources>
    ...
	<!-- String for the account type you want to use in your app -->
	<string name="auth_account_type">eu.unicate.retroauth.demo.account</string>
	<!-- String for the type of token you're using. -->
	<string name="auth_token_type">eu.unicate.retroauth.demo.token</string>
	<!-- String for the Action to open the activity to login if necessary -->
	<string name="authentication_action">eu.unicate.auth.action.AUTH</string>
	...
</resources>
```
### 2. Create an Activity (or use one you already have) where the user can login. This Activity must extend from AuthenticationActivity and call finalizeAuthentication when the authentication finished
i.e. (see Demo for an example)

```java
public class LoginActivity extends AuthenticationActivity {
   ...
   private void someLoginMethod() {
        String user;
        String token;
        Bundle additionalUserData; // nullable
        ... // do login work here and make sure, that you provide at least a user and a token String
        // the Token type is the one you defined in Step 1
        Account account = createOrGetAccount(user);<commit_msg>removed unnecessary bundle from readme file<commit_after># The simple way of calling authenticated requests in retrofit style
[![Build Status](https://travis-ci.org/Unic8/retroauth.svg?branch=master)](https://travis-ci.org/Unic8/retroauth)
## Dependencies
* [Retrofit](https://github.com/square/retrofit) 1.9.0
* [RxJava](https://github.com/ReactiveX/RxJava) 1.0.14
* appcompat-v7: 23.0.0

Min SDK Version: 9

## Example:
Your services using retrofit:
``` java
public interface SomeService {
    @GET("/some/path")
    Observable<ResultObject> someRxJavaCall();
}
```
Your services using retroauth:
``` java
@Authentication(accountType = R.string.auth_account_type, tokenType = R.string.auth_token_type)
public interface SomeService {
    @Authenticated
    @GET("/some/path")
    Observable<ResultObject> someAuthenticatedRxJavaCall();
}

```
## What does it do?
If you call a request method, annotated with the authenticated annotation, it'll do the following steps:
* Step 1: Checks if there already is an account in the Android AccountManager. If not, it'll open a LoginActivity (you choose which). If there already is an account, go on with step 2, If there's more than one account open an Dialog to pick an account.
* Step 2: Tries to get the authentication token from the (choosen) account to add it to the request header. If there is no valid token, your LoginActivity could open with the pre-filled accounts username. After login go to Step 1.
* Step 3: Sends the actual request
* Step 4: If the request fails with an 401 (by default, but changeable) it invalidates the used token in the Android AccountManager and continues with step 1.

Sequence Diagrams can be found in the DIAGRAMS.md file

## How to use it?
Add it as dependency:
```groovy
compile 'eu.unicate.android:retroauth:1.0.3'
```

### 1. Create 3 strings in your strings.xml
i.e.
``` xml
<resources>
    ...
	<!-- String for the account type you want to use in your app -->
	<string name="auth_account_type">eu.unicate.retroauth.demo.account</string>
	<!-- String for the type of token you're using. -->
	<string name="auth_token_type">eu.unicate.retroauth.demo.token</string>
	<!-- String for the Action to open the activity to login if necessary -->
	<string name="authentication_action">eu.unicate.auth.action.AUTH</string>
	...
</resources>
```
### 2. Create an Activity (or use one you already have) where the user can login. This Activity must extend from AuthenticationActivity and call finalizeAuthentication when the authentication finished
i.e. (see Demo for an example)

```java
public class LoginActivity extends AuthenticationActivity {
   ...
   private void someLoginMethod() {
        String user;
        String token;
        ... // do login work here and make sure, that you provide at least a user and a token String
        // the Token type is the one you defined in Step 1
        Account account = createOrGetAccount(user);
{'fraction_non_alphanumeric': 0.10056342837630186, 'fraction_numerical': 0.010927095782823972, 'mean_word_length': 4.076256499133449, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4749154', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1649, 'n_words': 685}