Jump to content

How to deal with jsonapi auth / token


pwFoo
 Share

Recommended Posts

I try to integrate Flarum forum with PW via JSONAPI. First tests are fine (register user, create discussions, get data, ...).

To get a valide token I send the username and password as a api call. If auth was successful I'll get the uid and a token back. This token have to be renewed after 30 minutes.

To get the token I need username and (plain!) password. So how should I save the user credentials / handle the users? 

  1. Just additional fields in the user profile
  2. username, password, token and uid as array (serialized + base64 encoded)
  3. Sync PW users with the remote app (hook PW auth and send a auth request via API call -> token returned = login OK)

1 and 2 would be flexible, but user credentials are saved as plain text!

3 is a secure solution (no plain credentials needed), but PW have to use a remote user backend / auth and maybe some things could be less flexible...

Do you see any problems with that solution? Could it break features / modules?

Link to comment
Share on other sites

I'll use user backend plugins.

Default is PW users (add fields to save flarum credentials). Additional planned plugin is Flarum user backend (hook PW login and check via Flarum API call).

First tests are fine. Syntax at the moment:

Create a discussion

// create a WireArray item, type "discussions" (= api endpoint)
$res = $flarum->makeBlankItem('discussions');

$res->title = "My discussion...";
$res->content = "My demo content";

// new discussion will be created by a api call
$res->save()

Get discussions, filter a object by PW api and update it...

// get discussions with pager (jsonapi filter is used)
$wireArray = $flarum->discussions->get(array('filter' => 'page[size]=3'));

// just use the PW api...
$discussion = $wireArray->get('title=MyTitle');

// modifiy the object
$discussion->title = "New title...";

// save to Flarum...
$discussion->save();

Delete a user

// delete a user by known id...
$flarum->users->delete($id);

// get users...
$flarumUsers = $flarum->users->get();  // all users

// get one user by ID | username
$flarumUser = $flarum->users->get(array('id' => $userId); // by userId
$flarumUser = $flarum->users->get(array('id' => 'MyUsername'); // by username

// delete a user object
$flarumUser->delete();
Link to comment
Share on other sites

  • 2 years later...

Hi @MadeMyDay,

I haven't finished the module because of some problems with api auth / token  of the used flarum version. 

On 4.1.2016 at 10:31 PM, pwFoo said:

To get a valide token I send the username and password as a api call. If auth was successful I'll get the uid and a token back. This token have to be renewed after 30 minutes.

To get the token I need username and (plain!) password.

Maybe it's easier with a current flarum version, but I haven't looked into it for a long time...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By jploch
      Hey folks,
      for a module (a pagebuilder based on PageTable) I need to save some settings as JSON. The values are saved for each page table item (a pw page). It's working well, but I am looking for ways to improve the structure I have. As I'm not that experienced with JSON, maybe someone more experienced can take a look and tell me if my approach is good practice. 

      My goal is to make all the items accessible by page id, without looping over them (using objects instead of arrays):
      // access from template with pw page var $jsonObject->items->{$page}->cssClass; Her is an example of my JSON structure:
      { "items": { "3252": { "id": "3252", "cssClass": "pgrid-main", "breakpoints": { "base": { "css": { "grid-column-end": "auto", "grid-row-end": "auto", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "60px", "padding-right": "60px", "padding-top": "60px", "padding-bottom": "60px", "background-color": "rgb(255, 255, 255)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3686": { "id": "3686", "cssClass": "test_global", "breakpoints": { "base": { "css": { "grid-column-end": "-1", "grid-row-end": "span 1", "grid-column-start": "1", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3687": { "id": "3687", "cssClass": "block_editor-3687", "breakpoints": { "base": { "css": { "grid-column-end": "span 2", "grid-row-end": "span 1", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3696": { "id": "3696", "cssClass": "block_editor-3696", "breakpoints": { "base": { "css": { "grid-column-end": "span 2", "grid-row-end": "span 1", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } } }, "breakpointActive": "base", "breakpointActiveSize": "@media (min-width: 576px)" }  
    • By benbyf
      HELLO! Anyone ever used Authy.com or Google authenticator on they processwire projects?
    • By pwFoo
      At the moment I do some tests with a jsonapi and wrote a simple client before I found the WireHttp module:
      https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/WireHttp.php
      jsonapi needs also PATCH (edit, update) and DELETE method. I opened an issue at github for that. Maybe Ryan could add these two methods.
        WireHttp isn't hookable. That was my second feature request, because it would be great to hook into the module to check response and renew the api token if needed.  patch() and delete() could be added as hook or by extend the WireHttp class, but added as native feature would be great.
      Anyone use the WireHttp class? I wrote a simple module to handle api requests before I found the WireHttp class. Would you recommend to switch to the WireHttp or use a custom json api module (based on file_get_contents() for example)?
    • By Mike Rockett
      Got home to some strange behaviour on one of my development sites - not able to login.
      I have seen this: processwire.com/talk/topic/4011-cannot-login-to-admin-area/
      But nothing there works. If I try changing the password, still can't sign in.
      Using SessionHandlerDatabase, and have cleared those caches too. Could that module be an issue in 2.5.25? I am running another few local sites on that version, but am not experiencing the same issue.
      Nothing in any error logs anywhere. Login form doesn't show any errors either.
      Wondering if the installation in question has gone all bonkers on me...
      Update: I also have the Forgot Password module enabled. Interestingly, when I click on it, it just shows the normal login form... Isn't it supposed to just show email? The URL does include ?forgot=1...
    • By GuruMeditation
      Thought some of you might find this interesting. Free and open source, and they will be providing migration scripts.

      https://www.kickstarter.com/projects/1221714515/flarum-forums-reimagined
×
×
  • Create New...