<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by dillan teagle on Medium]]></title>
        <description><![CDATA[Stories by dillan teagle on Medium]]></description>
        <link>https://medium.com/@dillan.teagle.va?source=rss-6fec457f462b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MNG8oxHioY7l-Dcw.</url>
            <title>Stories by dillan teagle on Medium</title>
            <link>https://medium.com/@dillan.teagle.va?source=rss-6fec457f462b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 07:56:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dillan.teagle.va/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Integrating Slack with Django Part One (‘Combining Slack OAuth with Active Directory…]]></title>
            <link>https://medium.com/@dillan.teagle.va/integrating-slack-with-django-part-one-combining-slack-oauth-with-active-directory-81d3a886cad7?source=rss-6fec457f462b------2</link>
            <guid isPermaLink="false">https://medium.com/p/81d3a886cad7</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[active-directory]]></category>
            <category><![CDATA[ldap]]></category>
            <category><![CDATA[slack]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[dillan teagle]]></dc:creator>
            <pubDate>Sat, 29 Jun 2019 17:47:41 GMT</pubDate>
            <atom:updated>2019-06-29T17:47:41.946Z</atom:updated>
            <content:encoded><![CDATA[<h3>Integrating Slack with Django Part One (‘Combining Slack OAuth with Active Directory Authenitcation’)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*jt9hA9eKMyIxkIPyaL1ypQ.png" /></figure><p>Many companies may use Active Directory for domain authentication. LDAP, a.k.a “Lightweight Directory Access Protocol”, is a communication language that applications use to communicate with other directory services servers.</p><p>With that said, we will be integrating two external API’s into the application, <a href="https://www.python-ldap.org/en/python-ldap-3.2.0/">python_ldap</a> and <a href="https://api.slack.com/">SlackAPI</a>. Now before we dig in, I want to clarify the use case and why this makes sense. A companies domain controller that is the gateway for user authentication, will also be listed as a a key: value pair in a Slack API response. Typically, your slack workspace will also identify with the name of your company domain. If that is the case, then using Slack as another form of authentication next to Active Directory makes sense, <strong>IF you have plans for integrating Slack within areas of your application!</strong></p><p>As far as interacting with Active Directory, fortunately there is already a library that is maintained that simplifies interacting with the <a href="https://www.python-ldap.org/en/python-ldap-3.2.0/">python_ldap api</a>.</p><p>So lets set up authentication to the domain controller first. Install <a href="https://django-auth-ldap.readthedocs.io/en/latest/">django_auth_ldap</a> and configure your settings.py.</p><p>At this step, you need to configure the settings to connect to the server where Active Directory runs, and configure your settings to bind, connect, and state how to search for user’s in the directory.</p><pre>AUTH_LDAP_SERVER_URI = config(&quot;active_directory_server&quot;)</pre><pre>AUTH_LDAP_CONNECTION_OPTIONS = {</pre><pre>ldap.OPT_DEBUG_LEVEL: 1,</pre><pre>ldap.OPT_REFERRALS: 0</pre><pre>}</pre><pre>AUTH_LDAP_BIND_DN = config(&quot;DN&quot;)</pre><pre>AUTH_LDAP_BIND_PASSWORD = config(&quot;PASSWORD&quot;)</pre><pre>AUTH_LDAP_USER_SEARCH = LDAPSearch(</pre><pre>&quot;CN=Users,DC=company,DC=test,DC=domain,DC=com&quot;, ldap.SCOPE_SUBTREE, &quot;sAMAccountName=%(user)s&quot;</pre><pre>)</pre><pre>AUTH_LDAP_USER_ATTR_MAP = {&quot;first_name&quot;: &quot;givenName&quot;, &quot;last_name&quot;: &quot;sn&quot;, &quot;username&quot;: &quot;sAMAccountName&quot;}</pre><pre>LDAP_IGNORE_CERT_ERRORS = True</pre><pre>AUTHENTICATION_BACKENDS = (</pre><pre>&#39;django_auth_ldap.backend.LDAPBackend&#39;,</pre><pre>)</pre><p>One thing that is important to mention is that I took out the default Django backend “ModelBackend”, for authenticating users, and replaced it with..</p><pre>django_auth_ldap.backend.LDAPBackend</pre><p>Once your settings are configured, you should be able to authenticate into your app with your credentials using Django’s login method. Seriously, its that simple.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a3de558d7120d93aa673d33514a04ca/href">https://medium.com/media/5a3de558d7120d93aa673d33514a04ca/href</a></iframe><p>For now ignore the try / except block, but pay attention to how I am using a login method off of our django form, if it is valid.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc72044a108e0929c992eda1944c35e5/href">https://medium.com/media/fc72044a108e0929c992eda1944c35e5/href</a></iframe><p>As you can see, I am setting Django’s authenticate method to use the LDAPBackend.</p><p>Now its time for the fun stuff, interacting with Slacks API which is a fantastic experience due to great documentation and structure.</p><blockquote>So for authenticating with Slack we will be using <a href="https://api.slack.com/docs/oauth">OAuth2.0</a> / Sign in with Slack. If you have not already created you slack app, please do so <a href="https://api.slack.com/apps?new_app=1">here</a>. Get your pretty Sign in with Slack button here.</blockquote><pre>&lt;a href=&quot;https://slack.com/oauth/authorize?scope=identity.basic&amp;client_id=your_client_id&quot;&gt;&lt;img src=&quot;https://api.slack.com/img/sign_in_with_slack.png&quot; /&gt;&lt;/a&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*yyvZ84hRVRllK6QcVS7zug.png" /></figure><p>I chose to set the Slack API credentials in a file that is within my settings directory.</p><pre>SLACK_CLIENT_ID = config(&quot;SLACK_CLIENT_ID&quot;)</pre><pre>SLACK_CLIENT_SECRET = config(&quot;SLACK_CLIENT_SECRET&quot;)</pre><pre>SLACK_AUTHORIZATION_URL = &#39;https://slack.com/oauth/authorize&#39;</pre><pre>SLACK_OAUTH_ACCESS = &#39;https://slack.com/api/oauth.access&#39;</pre><pre>SLACK_API_TOKEN = config(&quot;SLACK_API_TOKEN&quot;)</pre><p>Now it is time to think about our authorization flow. For the most part, Slack will take you through a redirect page that allows the user to authenticate with their Slack credentials against the workspace. So I chose to handle this with a RedirectView / “Class Based View”.</p><p>Before digging in to the view, lets set up how we are going to call the API.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1627513d23b737ff7e6421fc36ca4704/href">https://medium.com/media/1627513d23b737ff7e6421fc36ca4704/href</a></iframe><p>If you have read through Slack’s API docs and created your application, then you should understand the parameters used in our requests. The token is the access token obtained from ‘https://slack.com/api/oauth.access&#39; and scope defines permissions amongst the available endpoints.</p><p>Now setting up the authorization flow is your decision, but when you created and registered the slack app for your workspace, there is an option for a redirect uri. This is how slack knows where to deliver the user after they have handled the authentication.</p><p>Below is a RedirectView that handles the logic of the authorization flow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adca1b069cdb23e331403458b6fc95e6/href">https://medium.com/media/adca1b069cdb23e331403458b6fc95e6/href</a></iframe><p>Lets start with the get method, which is the primary method that combines all the functionality of the other helper methods together. So slack uses an authentication code that is exchanged for an access token. At <a href="https://slack.com/api/oauth.access">https://slack.com/api/</a>authorize this code is generated and swapped for an access code at <a href="https://slack.com/api/oauth.access">https://slack.com/api/oauth.access</a>.</p><p>The state parameter should be used to avoid forgery attacks by passing in a value that&#39;s unique to the user you&#39;re authenticating and checking it when auth completes. We store our state in the user session because of that reason.</p><p>If our API call is successful, then we will extract the information we want and return it in one dictionary.</p><pre>def gather_slack_info(user_object):</pre><pre>   slack_user = user_object[&quot;user&quot;]</pre><pre>   slack_team = user_object[&quot;team&quot;]</pre><pre>   print(slack_user)</pre><pre>   ldap_name = ldapify_name(slack_user[&quot;name&quot;])</pre><pre>   context = {</pre><pre>      &quot;slack_id&quot;: slack_user[&quot;id&quot;],</pre><pre>      &quot;name&quot;: slack_user[&quot;name&quot;],</pre><pre>      &quot;ldap_name&quot;: ldap_name,</pre><pre>      &quot;email&quot;: slack_user[&quot;email&quot;],</pre><pre>      &quot;team_id&quot;: slack_team[&quot;id&quot;],</pre><pre>      &quot;team_name&quot;: slack_team[&quot;name&quot;],</pre><pre>      &quot;domain&quot;: slack_team[&quot;domain&quot;],</pre><pre>      &quot;avatar&quot;: slack_user[&quot;image_24&quot;]</pre><pre>    }</pre><pre><em>    return</em> context</pre><p>ldapify_name will format the slack’s user name to match the format of username’s in our Active Directory.</p><pre>def ldapify_name(name):</pre><pre>    x = name.lower()</pre><pre>    y = x.replace(&quot; &quot;, &quot;&quot;)</pre><pre><em>    return</em> y</pre><p>In the last method of the RedirectView, we are sending the user back to the login screen if they do not exist in the database. User’s do not register in this application. So when you sign in for the first time using your credentials, you will then be added as a user with your active directory username and password.</p><p>I thought it was important that every user initially logs in to the application by LDAP for the first time, then they may use their workspace as an additional form of authentication in the future.</p><p>So I pass a keyword argument over to the login view that tells the django template to display a message to the user to login with Active Directory for their first time.</p><p>That is literally it. Next article will cover integrating the slack user within the application and customizing settings for sending notifications when your given tasks complete to where or whom you choose within the workspace.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81d3a886cad7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sending binary commands to devices in python]]></title>
            <link>https://medium.com/@dillan.teagle.va/sending-binary-commands-to-devices-in-python-60d64dc02c44?source=rss-6fec457f462b------2</link>
            <guid isPermaLink="false">https://medium.com/p/60d64dc02c44</guid>
            <category><![CDATA[tcp]]></category>
            <category><![CDATA[sockets]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[dillan teagle]]></dc:creator>
            <pubDate>Thu, 13 Jun 2019 23:03:55 GMT</pubDate>
            <atom:updated>2019-06-14T00:46:28.856Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sending commands to devices in python</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pDUQ_51DG-JewpIRzv29kw.jpeg" /></figure><p>I have recently started working with a company where its business model is wrapped around IOT devices in the field. I was recently tasked to write a python program that will send commands from a server to a device.</p><p>To send a command, we first must understand and choose what protocol we will use. I will be sending this command using TCP protocol, and building a packet that is encoded and eligible for delivery on behalf of another host.</p><p>Make sure that you have tested the tcp connection between both parties.</p><p>Verify that your designated port is open, then test the connection, it is possible that your ip tables / firewall rules could be blocking tcp connections from external hosts. To do this run an Ncat command,</p><pre>nc -vv address port</pre><p>Also you can run this command to listen on a port for an incoming connection</p><pre>nc -vlp 9100</pre><p>If all is good, you should receive a message that says the connection is established.</p><p>The first step is to create a socket and establish the connection between the two parties.</p><pre>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br>s.connect((dns, 9100))</pre><p>Once the connection has been established, its time to send a command. To send a packet, you must first encode the command. Most common use case is command.encode(“utf-8”)</p><p>In this case, I am storing the string value in a byte array and concatenating ASCII 27 and ASCII 13 at the beginning and end. Now its ready to be sent as a packet. I am setting the socket to half closed until I receive a response.</p><pre>packet = bytearray(&quot;\x1b&quot; + command + &quot;\x0d&quot;)</pre><pre>s.send(packet)<br>s.shutdown(socket.SHUT_WR)</pre><p>To retrieve the response, use s.recv() and the size of the packet will be noted in the method. Upon receiving the message, we parse the packet and now we have a response.</p><pre>result = s.recv(1024)</pre><pre>response = result.split(&#39;\0&#39;, 1)[0].strip()</pre><p>This example only shows a script that is used with Ansible to send commands to many devices. In order for the device to receive and respond, it will also need a python script that will accept the connection, receive, parse and respond.</p><pre><em>import</em> socket</pre><pre><em>import</em> sys</pre><pre>def send_command(dns, command):</pre><pre><em>try</em>:</pre><pre>    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</pre><pre>    s.connect((dns, 9100))</pre><pre><em>except</em> (socket.error,socket.herror,socket.gaierror) <em>as</em> (errno,<br>  <br>strErr):</pre><pre><em>    raise</em> Exception(&quot;Failed to connect to device&quot; + dns + &quot;: &quot; + <br> <br>    strErr)</pre><pre><em>try</em>:</pre><pre>    packet = bytearray(&quot;\x1b&quot; + command + &quot;\x0d&quot;)</pre><pre>    s.send(packet)</pre><pre>    s.shutdown(socket.SHUT_WR)</pre><pre>    result = s.recv(1024)</pre><pre>    response = result.split(&#39;\0&#39;, 1)[0].strip() </pre><pre>    print(response)</pre><pre><em>    return</em> response</pre><pre><em>except</em> (socket.error,socket.herror,socket.gaierror) <em>as</em> (errno,        strErr):</pre><pre><em>    raise</em> Exception(&quot;Failed to communicate with device&quot; +    dns   + </pre><pre>    &quot;: &quot; + strErr)</pre><pre><em>finally</em>:</pre><pre>    s.close()<br></pre><pre>def main():</pre><pre>    dns = sys.argv[1]</pre><pre>    command = sys.argv[2]</pre><pre>    send_command(dns, command)<br></pre><pre>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>Finally, I will run the file and pass in the required args.</p><pre>python name_of_file.py &quot;host&quot; &quot;command&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60d64dc02c44" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>