CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • API Access without logging in backend
Pages: [1] 2

Author Topic: API Access without logging in backend  (Read 11737 times)

bstalcup

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 1
  • CiviCRM version: 4.7.3
  • CMS version: Joomla!
  • MySQL version: 5.5.36-cll-lve
  • PHP version: 5.3.29
API Access without logging in backend
June 25, 2013, 09:08:03 am
Hello,

I'm currently working on developing a mobile app that will be able to access the CiviCRM Database (using the ajax API).  I understand that this requires CMS authentication (I'm using Joomla!), but I was wondering if you could do this without logging into the back end.  I'm not exactly sure how the AJAX authentication works, and what sort of login type is required.

Currently I've tried logging into the CMS through the frontend (as a Super User with all permissions), and the authentication doesn't work. However, when I log into the backend, and try to access the mobile app, the authentication DOES work.  I was wondering if there's a way to log into the front end and still having the authentication needed to access the API.  I've played with the ACL on Joomla!, but this hasn't helped at all.  Even when I grant a user ALL permissions in CiviCRM AND the rest of the site and they log into the front end, the user still cannot access the API.  The only way they can access the information in the mobile client (through the rest API), is if they're logged into the back end.  I was wondering if there's a way around this.

the site in question:
http://civicrmmobile.cloudaccess.net/

If you just log into the frontend, you cannot access the database from the mobile client, but if you log into the backend, you can.

Thanks for your time.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API Access without logging in backend
June 25, 2013, 09:32:20 pm
You should be able to if you add 'chek_permissions' = 0 to your call? See this issue:
http://issues.civicrm.org/jira/browse/CRM-8061

I am assuming you have read the CiviCRM Developer Guide chapter on the API? If not, please do: http://book.civicrm.org/developer/current/techniques/api/

Let me know if this helps in getting to the next step?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: API Access without logging in backend
June 26, 2013, 04:02:18 am
@bstalcup, if you create a contact who has permissions to access the contacts you need, you can access the JSON API using an API key.

Your mobile app needs three values - URL of API endpoint, site key (in civicrm.settings.php), api_key (in civicrm_contact table), API key means the permissions of the device owner can be applied.

Create URL with API endpoint, entity (type) & action: /civicrm/ajax/rest?entity=Contact&action=get

Then POST the rest of your values: api_key=abc&site_key=def&id=123

See class.api.php for examples (there are a few more settings like debug=1 & json=1 which you'll probably want to set).

The API guide in the developer handbook ("Calling the API from an external server via the REST API") talks about initialising a session, but AFAIK you can skip that and just post the API key every time so there's no need to store the session key locally or do session init.
@xurizaemon ● www.fuzion.co.nz

bstalcup

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 1
  • CiviCRM version: 4.7.3
  • CMS version: Joomla!
  • MySQL version: 5.5.36-cll-lve
  • PHP version: 5.3.29
Re: API Access without logging in backend
June 26, 2013, 09:50:58 am
I tried adding "&check_permissions=0" to the rest URL [http://[SITEURL]/administrator/?option=com_civicrm&task=civicrm/ajax/rest&check_permissions=0].  But that didn't help.  I was under the impression that when using the ajax rest, you did not need to add the api_key and key to the URL; it checks the user that is logged in to the CMS and is authorized there. 

Is this incorrect?   

I did take a look at the Developer Guide section on the API, and it helps me understand it a bit more, but obviously not enough to fix the problem at hand.  Are there any other fixes that I might be able to try?

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: API Access without logging in backend
June 26, 2013, 01:03:55 pm
check_permissions=0 only works from within the CMS (if you can execute php, the permissions system isn't going to help much)

Via http (to civicrm/ajax or rest.php) a flag like check_permissions=0 would bypass all of the built in security, which wouldn't be very good news :D

Instead those entry points require either a session ID already configured, or an API key.
@xurizaemon ● www.fuzion.co.nz

bstalcup

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 1
  • CiviCRM version: 4.7.3
  • CMS version: Joomla!
  • MySQL version: 5.5.36-cll-lve
  • PHP version: 5.3.29
Re: API Access without logging in backend
June 26, 2013, 01:09:37 pm
The plan for this was to have the user log into the CMS and be able to access the database if they were an authorized user (they have a particuler api_key attached to their name).  Would it be possible for a system like this to work?  Is this a case in which you could use check_permissions=0?

As a side note, are there any inherent flaws/security threats to this method?

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: API Access without logging in backend
June 26, 2013, 11:39:28 pm
It should be possible, I use the same kind of setup (but with Drupal) to synchronize data between different systems. I know the CiviMobile attempts use the API too. Security is not my topic....
In the setups I work with I do not use the check_permissions. Did you have a look at the CiviMobile code already present on GitHub?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API Access without logging in backend
June 27, 2013, 12:56:22 am
Hi,

I would avoid sharing the site key with too many "unstrusted" users, it's half of the info needed to get hacked.

I'd rely on the existing and well tested session system and use civicrm/ajax/rest

I'm not sure how it would work on joomla and the difference between front and backend login, but I imagine it's the same issue than for registering for an event of making a contribution and if it's possible to do it for these, it should be possible for the backend api, but might be some change needed. you might want to ask on IRC #civicm see if someone with joomla expertise can clarify

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

bstalcup

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 1
  • CiviCRM version: 4.7.3
  • CMS version: Joomla!
  • MySQL version: 5.5.36-cll-lve
  • PHP version: 5.3.29
Re: API Access without logging in backend
June 27, 2013, 07:58:18 am
The problem I'm encountering is that in Joomla you can log in as BOTH a user AND an admin (in different locations).  And you have to be logged in as an admin in order for the ajax authentication to work.  I was wondering if there was a way to authenticate a regular user, and not an admin. 

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: API Access without logging in backend
June 27, 2013, 03:31:22 pm
I'm confused on this too. Yes I have reviewed the above docs.  There are a bunch of docs about API's and authentication but I cannot find anything that spells out authentication for REST.  I found a contradiction I think ...
Above says you can use PHPSESSID but here - http://wiki.civicrm.org/confluence/display/CRMDOC43/API+changes it says you cannnot.

Also - Trouble finding a document that it explains how to generate an API key for each user.  Do you just add directly to the database?  Is it in the backend?  I'm surprised it safe to add a user/pass to the URL since the server can log it.
Finally, I want to use the quickserarch API - is there any documentation on it?  thanks.  S.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API Access without logging in backend
June 27, 2013, 04:03:32 pm
OK, they are two (confusingly named almost the same) methods:

the REST one in rest.php. it's made to let an external trusted server access civicrm and uses key+api_key as authentication

the ajax one, that uses civicrm/ajax/rest. it's made to be accessed from the browser by users that are already authenticated (and it uses the user session to authenticate)

In the REST, we strongly suggest to use POST, not GET so the key isn't in the url, but we don't ban the practice of using GET (yet at least). You should use post instead of get, it works all the time and they are little reasons of using get.

in ajax, we suggest to use the CRM.api wrapper, that puts the right params wherever needed so you don't have to think about it.

If you need all your users to be able to access ajax via the browser, you shouldn't use the rest (api key) interface but the ajax one.

They might be a fix needed to make civicrm/ajax/rest working in joomla via the front-end, would be great if you could investigate.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: API Access without logging in backend
June 27, 2013, 05:38:22 pm
Can you set up a POST method with a URL? Wanted to use Google Refine/OpenRefine and check if an email or name is in the DB.   This is quite useful if you are doing data cleansing: https://github.com/OpenRefine/OpenRefine/wiki/Fetching-URLs-From-Web-Services.  Basically you can tokenize a URL with GREL - but I can't seem to get it to work....

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: API Access without logging in backend
June 27, 2013, 07:38:18 pm
Quote from: sonicthoughts on June 27, 2013, 03:31:22 pm
Also - Trouble finding a document that it explains how to generate an API key for each user.  Do you just add directly to the database?  Is it in the backend?

Not aware of any documentation on this. As the API error says - Join the API (docs) team and implement it! (Document it, I mean!)

I actually had a fool around in civicrm/api/explorer the other day to see if I could coax it into setting or getting api_key, but it seems like that's not permitted ;)

Quote from: sonicthoughts
I'm surprised it safe to add a user/pass to the URL since the server can log it.

It's possible, but not safe. User/pass should be in POST only. Any sensitive data (creds, contact data) should be over SSL.

IMO it'd be good to specifically reject that usage in future versions, may already be a ticket on it @ issues.civicrm.org?
« Last Edit: June 27, 2013, 07:39:53 pm by Chris Burgess »
@xurizaemon ● www.fuzion.co.nz

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: API Access without logging in backend
June 27, 2013, 08:19:28 pm
Thanks Chris.  Well, I just went right into the database to add the user API key (used the hash value) - I imagine there is a better way.

FYI:  Still a bit stuck using REST with the quicksearch or getquick action (I've seen it referenced both ways).  I saw a fix in ajax here: http://issues.civicrm.org/jira/browse/CRM-11136 .
Wondering if, perhaps,  it was not applied to the rest api, but more likely my mistake somewhere.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API Access without logging in backend
June 27, 2013, 11:09:10 pm
Quote from: sonicthoughts on June 27, 2013, 08:19:28 pm
Thanks Chris.  Well, I just went right into the database to add the user API key (used the hash value) - I imagine there is a better way.

not so much, mostly because it's not meant to be done for a lot of users (I hardly ever have more than two users with api_key) and if a user can't modify the db directy, he most probably shouldn't use the api_key in the first place.

Quote from: sonicthoughts on June 27, 2013, 08:19:28 pm
FYI:  Still a bit stuck using REST with the quicksearch or getquick action (I've seen it referenced both ways).  I saw a fix in ajax here: http://issues.civicrm.org/jira/browse/CRM-11136 .
Wondering if, perhaps,  it was not applied to the rest api, but more likely my mistake somewhere.

getquick was a workaround, specifically to be automatically whitelisted to be accessible over http get. You can use it. But why aren't you using the normal contact.get api instead?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • API Access without logging in backend

This forum was archived on 2017-11-26.