SMS sending

This commit is contained in:
Jakob Aarøe Dam 2015-04-30 14:31:18 +02:00
parent 5b7c38ee96
commit f886f3af72
9 changed files with 91 additions and 34 deletions

View file

@ -7,12 +7,14 @@ Imports System.Security.Cryptography
Imports System.Text Imports System.Text
Imports System Imports System
Imports System.Net.Mail Imports System.Net.Mail
Imports System.Net.Http
Imports System.Web
Imports RADAR Imports RADAR
Public Class RDSFactor Public Class RDSFactor
Public Shared LDAPDomain As String = "" Public Shared LDAPDomain As String = ""
Public Shared ADField As String = "" Public Shared ADMobileField As String = ""
Public Shared ADMailField As String = "" Public Shared ADMailField As String = ""
Public Shared EnableOTP As Boolean Public Shared EnableOTP As Boolean
Public Shared secrets As New NASAuthList Public Shared secrets As New NASAuthList
@ -149,8 +151,8 @@ Public Class RDSFactor
ADMailField = RConfig.GetKeyValue("RDSFactor", "ADMailField") ADMailField = RConfig.GetKeyValue("RDSFactor", "ADMailField")
End If End If
ADField = RConfig.GetKeyValue("RDSFactor", "ADField") ADMobileField = RConfig.GetKeyValue("RDSFactor", "ADField")
If ADField.Length = 0 Then If ADMobileField.Length = 0 Then
LogInfo("ERROR: ADField can not be empty") LogInfo("ERROR: ADField can not be empty")
ConfOk = False ConfOk = False
End If End If
@ -217,40 +219,31 @@ Public Class RDSFactor
modem = Nothing modem = Nothing
Return "Ok" Return "Ok"
Else Else
LogDebug("Sending OTP: " & passcode & " to: " & number)
' TODO: Use HttpUtility UrlEncode when
' we figure out how to add the dll!!!
Dim url As String = Provider
url = url.Replace("***TEXTMESSAGE***", passcode)
url = url.Replace("***NUMBER***", number)
Dim baseurl As String = Provider.Split("?")(0) Dim client As New HttpClient
Dim client As New System.Net.WebClient()
' Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)") Dim response = client.GetAsync(url).Result
Dim content = response.Content.ReadAsStringAsync().Result
Dim parameters As String = Provider.Split("?")(1) If response.IsSuccessStatusCode Then
Dim pary As String() = parameters.Split("&")
For i As Integer = 0 To pary.Length - 1 If Not url.IndexOf("cpsms.dk") = -1 Then
If pary(i).IndexOf("***TEXTMESSAGE***") > 0 Then ' NOTE: Yes cpsms does indeed return HTTP 200 on errors!?!
Dim qpar As String() = pary(i).Split("=") If Not content.IndexOf("error") = -1 Then
client.QueryString.Add(qpar(0), passcode) Throw New SMSSendException(content)
ElseIf pary(i).IndexOf("***NUMBER***") > 0 Then End If
Dim qpar As String() = pary(i).Split("=")
client.QueryString.Add(qpar(0), number)
Else
Dim qpar As String() = pary(i).Split("=")
client.QueryString.Add(qpar(0), qpar(1))
End If End If
Next Else
Throw New SMSSendException(content)
End If
Dim data As Stream = client.OpenRead(baseurl)
Dim reader As New StreamReader(data)
Dim s As String = reader.ReadToEnd()
data.Close()
reader.Close()
Return (s)
End If End If
End Function End Function
Public Shared Function SendEmail(email As String, passcode As String) As String Public Shared Function SendEmail(email As String, passcode As String) As String

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform> <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
@ -14,7 +14,7 @@
<AssemblyName>RDSFactor</AssemblyName> <AssemblyName>RDSFactor</AssemblyName>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<MyType>Console</MyType> <MyType>Console</MyType>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<TargetFrameworkProfile> <TargetFrameworkProfile>
</TargetFrameworkProfile> </TargetFrameworkProfile>
<PublishUrl>publish\</PublishUrl> <PublishUrl>publish\</PublishUrl>
@ -42,6 +42,7 @@
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DocumentationFile>RDSFactor.xml</DocumentationFile> <DocumentationFile>RDSFactor.xml</DocumentationFile>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn> <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x86</PlatformTarget>
@ -52,6 +53,7 @@
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DocumentationFile>RDSFactor.xml</DocumentationFile> <DocumentationFile>RDSFactor.xml</DocumentationFile>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn> <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<OptionExplicit>On</OptionExplicit> <OptionExplicit>On</OptionExplicit>
@ -69,12 +71,21 @@
<ApplicationManifest>My Project\app.manifest</ApplicationManifest> <ApplicationManifest>My Project\app.manifest</ApplicationManifest>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\web\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration.Install" /> <Reference Include="System.Configuration.Install" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Deployment" /> <Reference Include="System.Deployment" />
<Reference Include="System.DirectoryServices" /> <Reference Include="System.DirectoryServices" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\web\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.ServiceProcess" /> <Reference Include="System.ServiceProcess" />
<Reference Include="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> <Reference Include="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
@ -94,8 +105,12 @@
<Import Include="System.Xml.Linq" /> <Import Include="System.Xml.Linq" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="exceptions\MissingEmail.vb" />
<Compile Include="exceptions\MissingNumber.vb" />
<Compile Include="exceptions\MissingLdapField.vb" />
<Compile Include="exceptions\MissingRadiusSecret.vb" /> <Compile Include="exceptions\MissingRadiusSecret.vb" />
<Compile Include="exceptions\MissingUser.vb" /> <Compile Include="exceptions\MissingUser.vb" />
<Compile Include="exceptions\SMSSendException.vb" />
<Compile Include="handlers\RDSHandler.vb" /> <Compile Include="handlers\RDSHandler.vb" />
<Compile Include="IniFile.vb" /> <Compile Include="IniFile.vb" />
<Compile Include="Log.vb" /> <Compile Include="Log.vb" />
@ -157,6 +172,7 @@
<CustomToolNamespace>My</CustomToolNamespace> <CustomToolNamespace>My</CustomToolNamespace>
<LastGenOutput>Settings.Designer.vb</LastGenOutput> <LastGenOutput>Settings.Designer.vb</LastGenOutput>
</None> </None>
<None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\radar-radius\RADAR\RADAR.vbproj"> <ProjectReference Include="..\radar-radius\RADAR\RADAR.vbproj">

View file

@ -0,0 +1,9 @@
Public Class MissingEmail
Inherits Exception
Public Sub New(ByVal user As String)
MyBase.New("User: " & user & " has no email")
End Sub
End Class

View file

@ -0,0 +1,9 @@
Public Class MissingLdapField
Inherits Exception
Public Sub New(field As String, username As String)
MyBase.New("No " & field & " entry in LDAP for " & username)
End Sub
End Class

View file

@ -0,0 +1,9 @@
Public Class MissingNumber
Inherits Exception
Public Sub New(ByVal user As String)
MyBase.New("User: " & user & " has no mobile number")
End Sub
End Class

View file

@ -0,0 +1,9 @@
Public Class SMSSendException
Inherits Exception
Public Sub New(ByVal message As String)
MyBase.New("SMS send error: " & message)
End Sub
End Class

5
server/packages.config Normal file
View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net451" />
</packages>

View file

@ -10,7 +10,8 @@ EndProject
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Pages", "http://localhost/RDWeb/Pages", "{B31FB884-C42E-417D-AF0D-409FE7D30351}" Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Pages", "http://localhost/RDWeb/Pages", "{B31FB884-C42E-417D-AF0D-409FE7D30351}"
ProjectSection(WebsiteProperties) = preProject ProjectSection(WebsiteProperties) = preProject
UseIISExpress = "false" UseIISExpress = "false"
TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.5" TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.5.1"
ProjectReferences = "{3ab08a4e-c4fa-4571-a5d4-32bba807c31d}|RADAR.dll;"
Debug.AspNetCompiler.VirtualPath = "/Pages" Debug.AspNetCompiler.VirtualPath = "/Pages"
Debug.AspNetCompiler.PhysicalPath = "RDWeb\Pages\" Debug.AspNetCompiler.PhysicalPath = "RDWeb\Pages\"
Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\Pages\" Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\Pages\"
@ -27,6 +28,10 @@ Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Pages", "http://localhost/R
Release.AspNetCompiler.Debug = "False" Release.AspNetCompiler.Debug = "False"
SlnRelativePath = "RDWeb\Pages\" SlnRelativePath = "RDWeb\Pages\"
EndProjectSection EndProjectSection
ProjectSection(ProjectDependencies) = postProject
{04C6C533-9FEA-41B2-B554-A166C7C7FE32} = {04C6C533-9FEA-41B2-B554-A166C7C7FE32}
{3AB08A4E-C4FA-4571-A5D4-32BBA807C31D} = {3AB08A4E-C4FA-4571-A5D4-32BBA807C31D}
EndProjectSection
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View file

@ -85,8 +85,10 @@
</authorization> </authorization>
</personalization> </personalization>
</webParts> </webParts>
<compilation targetFramework="4.5" debug="true"> <compilation targetFramework="4.5.1" debug="true">
<assemblies> <assemblies>
<add assembly="System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies> </assemblies>
</compilation> </compilation>