Jump to content

IPS 4.4 External Login


Cyber1

Recommended Posts

Hi guys, anyone know how to do external login on IPS 4.4 version?

Code below is working on IPS4.2 btw .. question is how to do it on newest ips version.

require_once(dirname(__FILE__).'/../forums/init.php');
$IPSLogin = new \IPS\Login\Internal;
$IPSLogin->init();

try {
         $member = $IPSLogin->authenticate(array('auth' => $_POST['username'], 'password' => $_POST['password']));
         if ($member) {
            $expire = new \IPS\DateTime;
            $expire->add( new \DateInterval( 'P7D' ) );
            \IPS\Request::i()->setCookie( 'member_id', $member->member_id, $expire );
            \IPS\Request::i()->setCookie( 'pass_hash', $member->member_login_key, $expire );
            \IPS\Request::i()->setCookie( 'hasJS', true, $expire );
            print '<html><head><meta http-equiv="refresh" content="0;URL=\''.$prev_url.'\'"></head></html>';
         }
      } catch (Exception $e) {
         blahblah
      }

 

Link to comment
Share on other sites

Here this might help you out.

 

<?php

class InvisionBridge {

    private static $instance;

    /**
     * InvisionAPI constructor.
     */
    public function __construct() {
        $this->requireIPS();
        $this->refreshSession();

        $this->session  = \IPS\Session::i();
        $this->database = \IPS\Db::i();
        $this->settings = \IPS\Settings::i();
    }

    /**
     * @return InvisionBridge
     * Constructs an instance of this class if one does not exist already.
     */
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new InvisionBridge();
        }
        return self::$instance;
    }

    /**
     * Returns the current logged in user
     * @return Member
     */
    public function getCachedMember() {
        \IPS\Session\Front::i(); // Refreshes our IPS session, if we ever had one.

        $member = \IPS\Member::loggedIn();

        if ($this->isGuest($member)) {
            return null;
        }

        return new Member($member);
    }

    /**
     * @param $username
     * @return Member|null
     */
    public function loadMember($username) {
        try {
            $member = \IPS\Member::load($username, 'name');

            if ($this->isGuest($member)) {
                return null;
            }

            return new Member($member);
        } catch (Exception $e) {
            return null;
        }
    }

    /**
     * @param int $member_id
     * @return Member|null
     */
    public function loadMemberById($member_id) {
        try {
            $member = \IPS\Member::load($member_id, 'member_id');

            if ($this->isGuest($member)) {
                return null;
            }

            return new Member($member);
        } catch (Exception $e) {
            return null;
        }
    }

    /**
     * @param $username
     * @param $password
     * @param bool $remember
     * @return Member|null
     * @throws Exception
     */
    public function login($username, $password, $remember = false) {
        $member = null;

        try {
            $member = $this->loadMember($username);
        } catch (Exception $e) {
            return null;
        }

        if ($member == null) {
            return null;
        }

        if (!$this->verifyPassword($member, $password)) {
            return null;
        }

        $this->setSession($member, $remember);
        return $member;
    }

    /**
     * Logs out of the current user.
     */
    public function logout() {
        $member = $this->getCachedMember();

        if ($member == null) {
            return; // We are already logged out
        }

        session_destroy();
        \IPS\Request::i()->clearLoginCookies();
        $member->getRaw()->memberSync('onLogout', array(\IPS\Http\Url::internal('')));
    }

    /**
     * Sets the user session after use has been verified.
     * @param $username
     * @return null
     * @throws Exception
     */
    public function setSession($member, $rememberMe) {
        \IPS\Session::i()->setMember($member->getRaw());

        $device = \IPS\Member\Device::loadOrCreate($member->getRaw());

        $member->getRaw()->last_visit = $member->last_activity;
        $member->getRaw()->save();

        $device->anonymous = false;
        $device->updateAfterAuthentication($rememberMe, null);

        $member->getRaw()->memberSync('onLogin');
        $member->getRaw()->profileSync();
    }

    /**
     * Checks if the user is a guest of not.
     * @param $member
     * @return bool
     */
    public function isGuest($member) {
        return $member->member_group_id == \IPS\Settings::i()->guest_group;
    }

    /**
     * Finds multi-factor authentication information of the specified type for the specified member.
     * @param $member
     * @param $type
     * @return null
     */
    public function findMfa($member, $type) {
        if ($member == null) {
            return null;
        }

        $mfaDetails = $member->getRaw()->get_mfa_details();

        if (count($mfaDetails) == 0) {
            return null;
        }

        $mfaToken = $mfaDetails[$type];

        if (isset($mfaToken)) {
            return $mfaToken;
        }

        return null;
    }

    /**
     * Finds and returns the latest topics in the specified board.
     * @param $boards array The board to find topics in
     * @param int $maximum The maximum amount of topics to find (default 100)
     * @return array Found topics, empty array if criteria is not matched
     */
    public function findLatestTopics($boards, $maximum = 100) {
        $query = "(forum_id = ".implode(' OR forum_id = ', $boards).")";

        $select = $this->database->select("*",
            'forums_topics',
            array("approved = 1 AND $query"),
            "start_date DESC",
            array(0, $maximum)
        );

        $topics = array();
        foreach ($select as $topic) {
            $topics[] = $topic;
        }
        return $topics;
    }

    /**
     * Finds and returns the latest posts in the specified topic.
     * @param $topic int The topic to find posts in
     * @param int $maximum The maximum amount of posts to find (default 100)
     * @return array Found posts, empty array if criteria is not matched
     */
    public function findPosts($topic, $maximum=100) {
        $select = $this->database->select("*",
            'forums_posts',
            array("topic_id = ?", $topic),
            "post_date ASC",
            array(0, $maximum)
        );

        $posts = array();
        foreach($select as $post) {
            $posts[] = $post;
        }
        return $posts;
    }

    /**
     * Finds and returns the latest posts in the specified topic.
     * @param $postId int The post id
     * @return array Found posts, empty array if criteria is not matched
     */
    public function findPost($postId) {
        $select = $this->database->select("*",
            'forums_posts',
            array("pid = ?", $postId),
            "post_date ASC"
        );
        return $select->first();
    }

    /**
     * Fetches a member's info directly from database to avoid loading using IPB files.
     * @param $mid integer
     * @return mixed
     */
    public function getMemberInfo($mid) {
        $select = $this->database->select("*",
            'core_members',
            array("member_id = ?", $mid),
            'member_id ASC',
            array(0, 1)
        );
        return $select->first();
    }

    /**
     * Verifies that the password entered is correct
     * @param $member
     * @param $password
     * @return bool
     */
    public function verifyPassword($member, $password) {
        return password_verify($password, $member->getPassword()) === true;
    }

    /**
     * Refreshes our IPS session, if we ever had one.
     * Required if for some reason our session has timed out and we have yet to revisit the suite.
     */
    public function refreshSession() {
        \IPS\Session\Front::i();
    }

    /**
     * Includes the required file from the forum
     */
    private function requireIPS() {
        require_once FORUM_PATH . 'init.php';
    }
}

 

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...
  • 1 month later...
On 7/20/2019 at 6:48 PM, S!r.ReaDy said:

If you want to use the external login with a website that supports API, ex VK/WordPress. Better use oAuth.

Alternative,

https://invisioncommunity.com/forums/topic/362329-remote-login/
 

Or try restApi.
 


 

yes tutorial in this link works for 4.1 and 4.2 but not working on 4.3 and up version.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...