OAuth を使用して IMAP、POP、SMTP および Exchange 接続を認証するのに役立つ修正が ColdFusion(2021 リリース)および ColdFusion(2018 リリース)向けにリリースされました。
このアップデートの適用方法
このアップデートを適用する前に、ColdFusion のどちらのバージョンにも最新のアップデートが適用されていることを確認してください。
ColdFusion インスタンスが複数ある場合は、インスタンスごとにアップデートを適用する必要があります。
ColdFusion(2021 リリース)
- patch.zip ファイルを解凍します。
- ColdFusion サーバーを停止します。
- <cf_home>/cfusion/bundles/mail-2021.0.05.330109.jar を mail-2021.0.05.330109.jar ファイルに置き換えます。
- <cf_home>/cfusion/lib/mail.jar を mail.jar に置き換えます。
- <cf_home>/cfusion/bin/felix-cache フォルダーをクリアします。
- jvm.config にフラグ -Dcoldfusion.mail.oauth2=true を追加します。
- ColdFusion サーバーを起動します。
ColdFusion(2018 リリース)
- ColdFusion サーバーを停止します。
- hf201800-4212004.jar ファイルを C:\ColdFusion2018\cfusion\lib\updates にコピーします。
- <cf_home>/cfusion/lib/ を mail.jar ファイルに置き換えます。
- jvm.config にフラグ -Dcoldfusion.mail.oauth2=true を追加します。
- ColdFusion サーバーを起動します。
認証コードを取得します。
アプリケーションの登録
OAuth を使用するには、アプリケーションを Azure Active Directory に登録します。詳しくは、クイックスタート:Microsoft ID プラットフォームにアプリケーションを登録するを参照してください。clientid、secretkey、authendpoint は、アプリケーションの登録後に取得されます。
<cfoauth clientid = "client-id" secretkey = "secret-key" scope="https://outlook.office.com/SMTP.Send" authendpoint="auth-end-point" accesstokenendpoint = "access-token-endpoint" result = "res" redirecturi = "http://localhost:8500/oauth/response.cfm">
アクセストークンを取得します。
<cfhttp result="res" url="https://login.microsoftonline.com/<auth-code>/oauth2/v2.0/token" method="post"> <cfhttpparam name="grant_type" type="formfield" value="authorization_code"> <cfhttpparam name="code" type="formfield" value="#code#"> <cfhttpparam name="client_id" type="formfield" value="client-id"> <cfhttpparam name="scope" type="formfield" value="https://outlook.office.com/SMTP.Send"> <cfhttpparam name="client_secret" type="formfield" value="secret-key"> <cfhttpparam name="redirect_uri" type="formfield" value="http://localhost:8500/oauth/response.cfm"> </cfhttp> <cfset record=deserializeJSON(res.Filecontent)/> <cfset token=#record.access_token#/>
アクセストークンを cfmail の password フィールドに渡します。
<cfmail server="smtp.office365.com" to="username@example.com" from="username@example.com" subject="#emailid#" port="587" username="#username#" password="#token#" spoolenable=false useTLS=true timeout=120> Hello, World! </cfmail>
<cfoauth clientid = "client-id" secretkey = "secret-key" scope="https://outlook.office.com/POP.AccessAsUser.All" authendpoint="auth-end-point" accesstokenendpoint = "access-token-endpoint" result = "res" redirecturi = http://localhost:8500/oauth/response.cfm>
アクセストークンを取得します。
<cfhttp result="res" url="https://login.microsoftonline.com/<auth-code>/oauth2/v2.0/token" method="post"> <cfhttpparam name="grant_type" type="formfield" value="authorization_code"> <cfhttpparam name="code" type="formfield" value="#code#"> <cfhttpparam name="client_id" type="formfield" value="client-id"> <cfhttpparam name="scope" type="formfield" value="https://outlook.office.com/POP.Send"> <cfhttpparam name="client_secret" type="formfield" value="secret-key"> <cfhttpparam name="redirect_uri" type="formfield" value="http://localhost:8500/oauth/response.cfm"> </cfhttp> <cfset record=deserializeJSON(res.Filecontent)/> <cfset token=#record.access_token#/>
アクセストークンを cfpop の password フィールドに渡します(下記を参照)。
<cfpop server = "outlook.office365.com" username = "#username#" password="#token#" port="995" action="GETALL" name="msg" secure="true">
<cfoauth clientid = "client-id" secretkey = "secret-key" scope="https://outlook.office.com/IMAP.AccessAsUser.All" authendpoint="auth-end-point" accesstokenendpoint = "access-token-endpoint" result = "res" redirecturi = http://localhost:8500/oauth/response.cfm>
アクセストークンを取得します。
<cfhttp result="res" url="https://login.microsoftonline.com/<auth-code>/oauth2/v2.0/token" method="post"> <cfhttpparam name="grant_type" type="formfield" value="authorization_code"> <cfhttpparam name="code" type="formfield" value="#code#"> <cfhttpparam name="client_id" type="formfield" value="client-id"> <cfhttpparam name="scope" type="formfield" value="https://outlook.office.com/IMAP.Send"> <cfhttpparam name="client_secret" type="formfield" value="secret-key"> <cfhttpparam name="redirect_uri" type="formfield" value="http://localhost:8500/oauth/response.cfm"> </cfhttp> <cfset record=deserializeJSON(res.Filecontent)/> <cfset token=#record.access_token#/>
アクセストークンを cfimap の password フィールドに渡します(下記を参照)。
<cfimap action="OPEN" connection="IMAPCONNECTION" server="outlook.office365.com" username="#username#" password="#token#" secure="true" stoponerror="false"/> <cfimap action="LISTALLFOLDERS" connection="IMAPCONNECTION" name="queryname" recurse=true stoponerror="false"/>
アップデートの適用方法を以下に示します。
ColdFusion(2021 リリース)
- exchange パッケージ JAR ファイルをダウンロードします。
- ColdFusion サーバーを停止します。
- <cf_home>/cfusion/bundles/exchange-2021.0.05.330109.jar を exchange-2021.0.05.330109.jar ファイルに置き換えます。
- C:\ColdFusion2021\cfusion\bin\felix-cache フォルダーをクリアします。
- jvm.config ファイルに jvm の引数「-Dcoldfusion.exchange.useOauth2=true」を設定します(デフォルトは false)。
- ColdFusion サーバーを起動します。
ColdFusion(2018 リリース)
- ColdFusion サーバーを停止します。
- hf201800-4212511.jar ファイルを <cf_home>/cfusion/lib/updates にコピーします。
- jvm.config ファイルに jvm の引数「-Dcoldfusion.exchange.useOauth2=true」を設定します(デフォルトは false)。
- ColdFusion サーバーを起動します。
認証済みの exchange 接続を取得するために cfexchangeconnection に渡すことができるアクセストークンを次のワークフローで生成します。
<cfoauth clientid = "your clientid" secretkey = "secret created in Azure portal when registering the application" scope = "https://outlook.office365.com/EWS.AccessAsUser.All" authendpoint = "https://login.microsoftonline.com/<tenant-ID>/oauth2/v2.0/authorize" accesstokenendpoint = "https://login.microsoftonline.com/<tenant-ID>/v2.0/token" result = "oresult" redirecturi = "http://localhost:8500/oauth/response.cfm">
response.cfm
<cfhttp result="httpresult" url="https://login.microsoftonline.com/<tenant-ID>/oauth2/v2.0/token" method="post"> <cfhttpparam name="grant_type" type="formfield" value="authorization_code"> <cfhttpparam name="scope" type="formfield" value="https://outlook.office365.com/EWS.AccessAsUser.All"> <cfhttpparam name="code" type="formfield" value="#url.code#"> <cfhttpparam name="client_id" type="formfield" value="your clientid"> <cfhttpparam name="redirect_uri" type="formfield" value="http://localhost:8500/oauth/response.cfm"> </cfhttp> <cfdump var=#res.Filecontent#> // この応答内の access_token には cfexchange タグに渡された access_token が含まれている <cfexchangeconnection action="open" username="username@domain.com" password="#auth-token#" server="outlook.office365.com" protocol="https" serverversion="2010_SP2" connection="exchangeconn">
別のユーザーとして実行する場合は、-Dcoldfusion.exchange.setImpersonatedUser=true
のようにフラグを true に設定します(デフォルトは false)。