/*
 * Copyright 2014 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.model.login;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * The user's credentials.
 */
public class Credentials {
    public String userId;
    public String homeServer;  // This is the server name and not a URI, e.g. "matrix.org"
    public String accessToken;

    public JSONObject toJson() throws JSONException {
        JSONObject json = new JSONObject();

        json.put("user_id", userId);
        json.put("home_server", homeServer);
        json.put("access_token", accessToken);

        return json;
    }

    public static Credentials fromJson(JSONObject obj) throws JSONException {
        Credentials creds = new Credentials();
        creds.userId = obj.getString("user_id");
        creds.homeServer = obj.getString("home_server");
        creds.accessToken = obj.getString("access_token");
        return creds;
    }

    @Override
    public String toString() {
        return "Credentials{" +
                "userId='" + userId + '\'' +
                ", homeServer='" + homeServer + '\'' +
                ", accessToken.length='" + (accessToken != null ? accessToken.length() : "null") + '\'' +
                '}';
    }
}

@@ -640,16 +640,84 @@
 login;%0A%0A
+import android.text.TextUtils;%0A%0Aimport com.google.gson.JsonObject;%0A%0A
 import o
@@ -766,16 +766,46 @@
 NObject;
+%0Aimport org.json.JSONStringer;
 %0A%0A/**%0A *
@@ -1007,16 +1007,48 @@
 ssToken;
+%0A    public String refreshToken;
 %0A%0A    pu
@@ -1269,16 +1269,116 @@
 Token);%0A
+        json.put(%22refresh_token%22, TextUtils.isEmpty(refreshToken) ? JSONObject.NULL : refreshToken);
 %0A       
@@ -1684,24 +1684,381 @@
 ss_token%22);%0A
+%0A        // refresh_token is mandatory%0A        if (obj.has(%22refresh_token%22)) %7B%0A            try %7B%0A                creds.refreshToken = obj.getString(%22refresh_token%22);%0A            %7D catch (Exception e) %7B%0A                creds.refreshToken = null;%0A            %7D%0A        %7D else %7B%0A            throw new RuntimeException(%22refresh_token is required.%22);%0A        %7D%0A%0A
         retu
@@ -2243,32 +2243,140 @@
 Server + '%5C'' +%0A
+                %22, accessToken.length='%22 + (refreshToken != null ? refreshToken.length() : %22null%22) + '%5C'' +%0A
                 

Add the refresh_token field in the credentials.