What is the correct way to use UserIdentity? I am planning to create a micropub plugin so that any micropub client can post to craft, particularly useful for automatic data logging and mobile clients. But I am trying to work out the best way to authenticate.

I have read through the UserIdentity docs but I was wondering if anybody has an examples of how to use it correctly?
The basic user identity relies on login and password, so you will have to create your own class to represent an identity based on something else, like a token for example:

&lt;?php

namespace Craft;

class TokenIdentity extends UserIdentity
{
    private $_id;
    public $token;

    public function __construct($token)
    {
        $this-&gt;token = $token;
    }

    public function authenticate()
    {
        $socialUser = craft()-&gt;social-&gt;getUserByEncodedToken($this-&gt;token);

        if($socialUser) {
            $this-&gt;_id = $socialUser-&gt;user-&gt;id;
            $this-&gt;username = $socialUser-&gt;user-&gt;username;
            $this-&gt;errorCode = static::ERROR_NONE;

            return true;
        } else {
            return false;
        }
    }

    public function getId()
    {
        return $this-&gt;_id;
    }
}


Then you'll need to create your own user session service to be able to authenticate the user (with a token for example):

&lt;?php

namespace Craft;

class Social_UserSessionService extends UserSessionService {

    private $_identity;
    public $allowAutoLogin = true;

    public function init()
    {
        $this-&gt;setStateKeyPrefix(md5('Yii.Craft\UserSessionService.'.craft()-&gt;getId()));

        parent::init();
    }

    public function login(Oauth_TokenModel $token)
    {
        $rememberMe = true;

        $this-&gt;_identity = new TokenIdentity($token-&gt;encodedToken);
        $this-&gt;_identity-&gt;authenticate();

        // Was the login successful?
        if ($this-&gt;_identity-&gt;errorCode == TokenIdentity::ERROR_NONE)
        {
            // Get how long this session is supposed to last.

            $this-&gt;authTimeout = craft()-&gt;config-&gt;getUserSessionDuration($rememberMe);

            $id = $this-&gt;_identity-&gt;getId();

            $user = craft()-&gt;users-&gt;getUserById($id);

            $states = $this-&gt;_identity-&gt;getPersistentStates();


            // Run any before login logic.
            if ($this-&gt;beforeLogin($id, $states, false))
            {
                // Fire an 'onBeforeLogin' event
                $this-&gt;onBeforeLogin(new Event($this, array(
                    'username'      =&gt; $user-&gt;username,
                )));

                $this-&gt;changeIdentity($id, $this-&gt;_identity-&gt;getName(), $states);

                // Fire an 'onLogin' event
                $this-&gt;onLogin(new Event($this, array(
                    'username'      =&gt; $user-&gt;username,
                )));

                if ($this-&gt;authTimeout)
                {
                    if ($this-&gt;allowAutoLogin)
                    {
                        if ($user)
                        {
                            // Save the necessary info to the identity cookie.
                            $sessionToken = StringHelper::UUID();
                            $hashedToken = craft()-&gt;security-&gt;hashData(base64_encode(serialize($sessionToken)));
                            $uid = craft()-&gt;users-&gt;handleSuccessfulLogin($user, $hashedToken);

                            $data = array(
                                $this-&gt;getName(),
                                $sessionToken,
                                $uid,
                                ($rememberMe ? 1 : 0),
                                craft()-&gt;request-&gt;getUserAgent(),
                                $this-&gt;saveIdentityStates(),
                            );

                            $this-&gt;saveCookie('', $data, $this-&gt;authTimeout);
                        }
                        else
                        {
                            throw new Exception(Craft::t('Could not find a user with Id of {userId}.', array('{userId}' =&gt; $this-&gt;getId())));
                        }
                    }
                    else
                    {
                        throw new Exception(Craft::t('{class}.allowAutoLogin must be set true in order to use cookie-based authentication.', array('{class}' =&gt; get_class($this))));
                    }
                }

                $this-&gt;afterLogin(false);
            }

            return !$this-&gt;getIsGuest();
        }

        Craft::log('Tried to log in unsuccessfully.', LogLevel::Warning);
        return false;
    }
}


This code comes from a plugin which is still under development so don't take for granted it's all secure or the best way to achieve this, but hopefully it will help you get started !