<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://rt-wiki.bestpractical.com/index.php?action=history&amp;feed=atom&amp;title=Rt-auth-user</id>
	<title>Rt-auth-user - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://rt-wiki.bestpractical.com/index.php?action=history&amp;feed=atom&amp;title=Rt-auth-user"/>
	<link rel="alternate" type="text/html" href="https://rt-wiki.bestpractical.com/index.php?title=Rt-auth-user&amp;action=history"/>
	<updated>2026-08-23T12:06:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>https://rt-wiki.bestpractical.com/index.php?title=Rt-auth-user&amp;diff=3244&amp;oldid=prev</id>
		<title>Admin: 5 revisions imported</title>
		<link rel="alternate" type="text/html" href="https://rt-wiki.bestpractical.com/index.php?title=Rt-auth-user&amp;diff=3244&amp;oldid=prev"/>
		<updated>2016-04-06T20:36:20Z</updated>

		<summary type="html">&lt;p&gt;5 revisions imported&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Perl script authenticating users against RT==&lt;br /&gt;
UPDATE: this script is OK but a much cleaner solution to the problem mentioned below is using RT&amp;#039;s [[REST|REST interface]].&lt;br /&gt;
Requires [http://search.cpan.org/~tsibley/RT-Authen-ExternalAuth/ RT::Authen::ExternalAuth] (but you may also do without it if you need only local auth).&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
We have many different web applications and an RT installation that we have configured to let a lot of users login (e.g. the sources of RT-Authen-ExternalAuth are 2 LDAP servers and 1 external DB). The users of each web application are a subset of RT&amp;#039;s users. So, we would like to use RT as a central authentication service.&lt;br /&gt;
&lt;br /&gt;
===Solution===&lt;br /&gt;
A perl script that calls RT&amp;#039;s APIs, and tries to authenticate a user in 2 steps:&lt;br /&gt;
#Against all the auth sources we configured for [http://search.cpan.org/~tsibley/RT-Authen-ExternalAuth/ RT::Authen::ExternalAuth]&lt;br /&gt;
#Against the local RT&amp;#039;s DB&lt;br /&gt;
&lt;br /&gt;
===Code===&lt;br /&gt;
 #!/usr/bin/perl -w&lt;br /&gt;
 &lt;br /&gt;
 use lib qw(/opt/rt4/lib /opt/rt4/local/plugins/RT-Authen-ExternalAuth/lib);&lt;br /&gt;
 &lt;br /&gt;
 #use strict;&lt;br /&gt;
 use RT::Interface::CLI qw(CleanEnv GetMessageContent loc);&lt;br /&gt;
 &lt;br /&gt;
 CleanEnv();       # Clean our the environment&lt;br /&gt;
 use RT;&lt;br /&gt;
 RT::LoadConfig(); # Load the RT configuration&lt;br /&gt;
 RT::Init();       # Initialise RT&lt;br /&gt;
 &lt;br /&gt;
 use Getopt::Long;&lt;br /&gt;
 my $username = &amp;#039;&amp;#039;;&amp;#039;&amp;#039;&lt;br /&gt;
 my $pass = &amp;#039;&amp;#039;;&amp;#039;&amp;#039;&lt;br /&gt;
 GetOptions (&amp;quot;user=s&amp;quot; =&amp;gt; \$username,&lt;br /&gt;
             &amp;quot;pass=s&amp;quot;   =&amp;gt; \$pass);&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 use RT::Authen::ExternalAuth;&lt;br /&gt;
 &lt;br /&gt;
 my $result=&amp;quot;&amp;quot;;&lt;br /&gt;
 my %session;&lt;br /&gt;
 my ($val,$msg) = RT::Authen::ExternalAuth::DoAuth(\%session,$username,$pass);&lt;br /&gt;
 #$RT::Logger-&amp;gt;debug(&amp;quot;Someone called ExternalAuth. Response: ($val, $msg)&amp;quot;);&lt;br /&gt;
 $result = $val;&lt;br /&gt;
 &lt;br /&gt;
 unless($result == 1) {&lt;br /&gt;
   my $user = new RT::User($RT::SystemUser);&lt;br /&gt;
   $user-&amp;gt;Load($username);&lt;br /&gt;
 &lt;br /&gt;
   if($user-&amp;gt;IsPassword($pass)) {&lt;br /&gt;
     $result = &amp;quot;1&amp;quot;;&lt;br /&gt;
   } else {&lt;br /&gt;
     $result = &amp;quot;0&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 print STDOUT $result;&lt;br /&gt;
 exit;&lt;br /&gt;
&amp;lt;span style=&amp;quot;line-height:13.999999046325684px;white-space:pre;&amp;quot;&amp;gt; &amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Usage===&lt;br /&gt;
&lt;br /&gt;
 rt-auth-user --user $username --pass $password&lt;br /&gt;
===(Std) output===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1&amp;quot; if auth succeeds; &amp;quot;0&amp;quot; otherwise.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Client example (PHP)===&lt;br /&gt;
&lt;br /&gt;
 ​function auth_rt($username, $password) {&lt;br /&gt;
         # check the user trying to authenticate is whitelisted&lt;br /&gt;
         $user_row = db_getUser($username);&lt;br /&gt;
         if(empty($user_row)) return false;&lt;br /&gt;
 &lt;br /&gt;
         # now check the credentials are correct&lt;br /&gt;
         global $RT_HOST;&lt;br /&gt;
         $cmd = &amp;quot;/opt/rt4/sbin/custom/rt-auth-user --user $username --pass $password&amp;quot;;&lt;br /&gt;
         $result = shell_exec(&amp;quot;ssh selfservice@$RT_HOST $cmd&amp;quot;);&lt;br /&gt;
         if( $result == 0 ) return false;&lt;br /&gt;
         return $user_row;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This PHP function authenticates a user against an RT installation.&lt;br /&gt;
&lt;br /&gt;
Note. In the first lines we have implemented an authorization mechanism based on a whitelist of users that are able to login to our PHP application.&lt;br /&gt;
&lt;br /&gt;
Note2. This function is supposed to have input validated by some other methods (eg. in the Model class), as a &amp;#039;&amp;#039;&amp;#039;precondition&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
	<entry>
		<id>https://rt-wiki.bestpractical.com/index.php?title=Rt-auth-user&amp;diff=3242&amp;oldid=prev</id>
		<title>151.91.34.12: /* Client example (PHP) */</title>
		<link rel="alternate" type="text/html" href="https://rt-wiki.bestpractical.com/index.php?title=Rt-auth-user&amp;diff=3242&amp;oldid=prev"/>
		<updated>2013-01-01T19:10:02Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Client example (PHP)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Perl script authenticating users against RT==&lt;br /&gt;
Requires [http://search.cpan.org/~tsibley/RT-Authen-ExternalAuth/ RT::Authen::ExternalAuth] (but you may also do without it if you need only local auth).&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
We have many different web applications and an RT installation that we have configured to let a lot of users login (e.g. the sources of RT-Authen-ExternalAuth are 2 LDAP servers and 1 external DB). The users of each web application are a subset of RT&amp;#039;s users. So, we would like to use RT as a central authentication service.&lt;br /&gt;
&lt;br /&gt;
===Solution===&lt;br /&gt;
A perl script that calls RT&amp;#039;s APIs, and tries to authenticate a user in 2 steps:&lt;br /&gt;
#Against all the auth sources we configured for [http://search.cpan.org/~tsibley/RT-Authen-ExternalAuth/ RT::Authen::ExternalAuth]&lt;br /&gt;
#Against the local RT&amp;#039;s DB&lt;br /&gt;
&lt;br /&gt;
===Code===&lt;br /&gt;
 #!/usr/bin/perl -w&lt;br /&gt;
 &lt;br /&gt;
 use lib qw(/opt/rt4/lib /opt/rt4/local/plugins/RT-Authen-ExternalAuth/lib);&lt;br /&gt;
 &lt;br /&gt;
 #use strict;&lt;br /&gt;
 #use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);&lt;br /&gt;
 use RT::Interface::CLI qw(CleanEnv GetMessageContent loc);&lt;br /&gt;
 &lt;br /&gt;
 CleanEnv();       # Clean our the environment&lt;br /&gt;
 use RT;&lt;br /&gt;
 RT::LoadConfig(); # Load the RT configuration&lt;br /&gt;
 RT::Init();       # Initialise RT&lt;br /&gt;
 &lt;br /&gt;
 use Getopt::Long;&lt;br /&gt;
 my $username = &amp;#039;&amp;#039;;&amp;#039;&amp;#039;&lt;br /&gt;
 my $pass = &amp;#039;&amp;#039;;&amp;#039;&amp;#039;&lt;br /&gt;
 GetOptions (&amp;quot;user=s&amp;quot; =&amp;gt; \$username,&lt;br /&gt;
             &amp;quot;pass=s&amp;quot;   =&amp;gt; \$pass);&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 use RT::Authen::ExternalAuth;&lt;br /&gt;
 &lt;br /&gt;
 my $result=&amp;quot;&amp;quot;;&lt;br /&gt;
 my %session;&lt;br /&gt;
 my ($val,$msg) = RT::Authen::ExternalAuth::DoAuth(\%session,$username,$pass);&lt;br /&gt;
 #print $msg;&lt;br /&gt;
 #$RT::Logger-&amp;gt;debug(&amp;quot;Someone called ExternalAuth. Response: ($val, $msg)&amp;quot;);&lt;br /&gt;
 $result = $val;&lt;br /&gt;
 &lt;br /&gt;
 unless($result == 1) {&lt;br /&gt;
   my $user = new RT::User($RT::SystemUser);&lt;br /&gt;
   $user-&amp;gt;Load($username);&lt;br /&gt;
 &lt;br /&gt;
   if($user-&amp;gt;IsPassword($pass)) {&lt;br /&gt;
     $result = &amp;quot;1&amp;quot;;&lt;br /&gt;
   } else {&lt;br /&gt;
     $result = &amp;quot;0&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 print STDOUT $result;&lt;br /&gt;
 exit;&lt;br /&gt;
&amp;lt;span style=&amp;quot;line-height:13.999999046325684px;white-space:pre;&amp;quot;&amp;gt; &amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Usage===&lt;br /&gt;
&lt;br /&gt;
 rt-auth-user --user $username --pass $password&lt;br /&gt;
===(Std) output===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1&amp;quot; if auth succeeds; &amp;quot;0&amp;quot; otherwise.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Client example (PHP)===&lt;br /&gt;
&lt;br /&gt;
 ​function auth_rt($username, $password) {&lt;br /&gt;
         # check the user trying to authenticate is whitelisted&lt;br /&gt;
         $user_row = db_getUser($username);&lt;br /&gt;
         if(empty($user_row)) return false;&lt;br /&gt;
 &lt;br /&gt;
         # now check the credentials are correct&lt;br /&gt;
         global $RT_HOST;&lt;br /&gt;
         $cmd = &amp;quot;/opt/rt4/sbin/custom/rt-auth-user --user $username --pass $password&amp;quot;;&lt;br /&gt;
         $result = shell_exec(&amp;quot;ssh selfservice@$RT_HOST $cmd&amp;quot;);&lt;br /&gt;
         if( $result == 0 ) return false;&lt;br /&gt;
         return $user_row;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This PHP function authenticates a user against an RT installation.&lt;br /&gt;
&lt;br /&gt;
Note. In the first lines we have implemented an authorization mechanism based on a whitelist of users that are able to login to our PHP application.&lt;br /&gt;
&lt;br /&gt;
Note2. This function is supposed to have input validated by some other methods (perheps around the View layer), as a &amp;#039;&amp;#039;&amp;#039;precondition&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>151.91.34.12</name></author>
	</entry>
</feed>