Login to Odoo from external php system
<p>I have a requirement where I need to have a redirect from the external php system to Odoo, and the user should be logged in as well. I thought of the following two ways to get this done:</p>

<ol>
<li><p>A url redirection from the php side which calls a particular controller,and pass the credentials alongiwth the url, which is not a secure option for obvious reasons</p></li>
<li><p>A call of method using xmlrpc from php, and pass the necessary arguments along from php, use the arguments to sign in and then in the method over here a call for redirect is made. Will have to check further whether this method will work, as the controller and normal functions work in different ways when it comes to redirections within odoo.</p></li>
</ol>

<p>Please suggest as to which way would be better or are there any other ways to get this done which might be simpler. And, would it make sense to add a new method in the openerp/service/common.py and call that method, and then will it be possible to redirect to the odoo logged in page from there?</p>

<p>Hoping for inputs on the above, and I also hope that this helps out with other external system integration queries which are frequent in Odoo.</p>

<p>Thanks And Regards,
Yaseen Shareef</p>

<p>In your php code you could make a jsonrpc call to <code>/web/session/authenticate</code> and receive the session_id in the response. You could pass the session_id as the hash of your url in your redirect. Create a page in odoo that uses javascript to read the hash and write the cookie <code>"session_id=733a54f4663629ffb89d3895f357f6b1715e8666"</code> (obviously an example) to your browser on your odoo page. At this point you should be able to navigate in odoo as the user you authenticated as in your php code. </p>

<pre><code>    from requests import Request,Session
    import json

    base_url = "127.0.0.1:8069"
    url = "%s/web/session/authenticate" % base_url
    db = &lt;db_name&gt;
    user = &lt;login&gt;
    passwd = &lt;password&gt;

    s = Session()

    data = {
        'jsonrpc': '2.0',
        'params': {
            'context': {},
            'db': db,
            'login': user,
            'password': passwd,
        },
    }

    headers = {
        'Content-type': 'application/json'
    }

    req = Request('POST',url,data=json.dumps(data),headers=headers)
    prepped = req.prepare()
    resp = s.send(prepped)

    r_data = json.loads(resp.text)
    session_id = r_data['result']['session_id']
</code></pre>

<p>This example uses vanilla curl. Which I know is not php however I may update this post later for php. The principle still stands however. Just convert this curl to php.     </p>

<p>How you pass this session_id with your redirect is up to you. There are security concerns which you will need to be wary of. So be careful not to pass the session_id insecurely or someone could sniff it and become your logged in user.</p>

<p>This is an example (untested), you will have to create json encoded string similar to the curl example I provided above. Hope this helps.</p>

<pre><code>$data = &lt;your_json_data&gt;
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://127.0.0.1:8069/web/session/authenticate');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);
</code></pre>

<p>Once you have your <code>session_id</code> you will redirect to your odoo server to a route which is handled by a controller in your addon.</p>

<p>Controller</p>

<pre><code>imports ...

class MyAddon(http.Controller):

@http.route('/path/for/controller', type='http', auth='public', website=True)
def ext_login_redirect(self, **kw):
    return http.request.render('myaddon.template',{})
</code></pre>

<p>The important part is that the hash in the url contains the session_id you obtained in your php.</p>

<p>Your template <code>your_template.xml</code></p>

<pre><code>&lt;openerp&gt;
    &lt;data&gt;
        &lt;template id="template" name="Redirect Template" page="True"&gt;
            document.cookie = "session_id=" + window.location.hash;
            window.location = "127.0.0.1:8069/web";
        &lt;/template&gt;
    &lt;/data&gt;
&lt;/openerp&gt;
</code></pre>

python openerp xml-rpc odoo-9