Discourse SSO and API Helper for PHP

AI-generated summary

The discussion centers around a PHP package for Discourse SSO and API integration, developed by vinothkannans, available via Composer as vinkas/discourse-php. It supports both API interactions (e.g., topic creation) and SSO authentication using GuzzleHttp and Composer, with examples provided. vinothkannans admits the code lacks thorough testing and documentation but plans to improve it in a future release. The package also serves as a dependency for a Laravel-specific package he is developing. erlend_sh notes potential overlap with michaeld’s existing PHP API client, which michaeld criticizes as unnecessary duplication, suggesting contributions to existing projects instead. vinothkannans defends his decision, citing structural differences and personal use-case needs. Later, pnoeric, writing in March 2020, notes that michaeld’s library doesn’t support SSO and finds vinothkannans’ package buggy and immature, recommending cviebrock/discourse-php as a more stable alternative for current use.

Installation

The package is registered at Packagist as vinkas/discourse-php and can be installed using composer:

composer require vinkas/discourse-php

Usage

$discourse = new Vinkas\Discourse\PHP\Client('discouse.example.com', true);  // set true if ssl enabled

API

$api = $discourse->api('API_KEY', 'API_USERNAME');
$api->topics()->create('TITLE', 'CONTENT', 'CATEGORY_SLUG')

SSO

$payload = $_GET['sso'];
$signature = $_GET['sig'];

$sso = $discourse->sso('SECRET', $payload, $signature);

if (!($sso->isValid())) {
    header("HTTP/1.1 403 Forbidden");
    echo("Bad SSO request");
    die();
}

$userParams = array(
    'external_id' => 'USER_ID',
    'email'     => 'EMAIL_ADDRESS',
    'username' => 'USERNAME',  // optional
    'name'     => 'FULL_NAME'  // optional
    // for more available fields https://meta.discourse.org/t/official-single-sign-on-for-discourse/13045
);

$url = $sso->getResponseUrl($userParams)
header('Location: ' . $url);
exit(0);
6 лайков

Since I committed in other task I didn’t tested the code well and unable to give detailed documentation with usage examples.

I will release next version soon with more options, documentation and examples.

1 лайк

I haven’t looked closely at this but it sounds like there might be some overlap with @michaeld’s PHP API client for Discourse:

https://github.com/communiteq/discourse-api-php

I already aware of this existing package. But I didn’t used it. I used famous GuzzleHttp (they used php native curl) and composer. Also in mine it have SSO option too.

After all I created this package because of I am also creating laravel package (which depends it) for easy implementation with laravel features like eloquent model. Yes few laravel packages also already exist for discourse. But I have something different in my mind for development :slight_smile:

1 лайк

Too bad you decided to create another, instead of just contributing to what already existed, and making it better.

Sorry @michaeld. Coding structure which I wanted and created is totally different from your existing package. That’s why I created it as new one.

In other words I created this package for my own purpose. Then I decided to open source it.

1 лайк

Для тех, кто только сейчас наткнулся на эту тему (сейчас март 2020 года… привет из домашней карантина по COVID-19!) —

Библиотека от @michaeld, упомянутая выше, судя по всему, предназначена только для работы с API. Это никак не связано с SSO.

Я пробовал использовать библиотеку от @vinothkannans, которую он поделил на Github, но, как он сам отметил выше, он не проводил достаточного тестирования. Действительно, я обнаружил, что она довольно глючная и пока не готова к использованию в продакшене.

Я нашел этот вариант, и он отлично работает у меня:

9 лайков