Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PPT-903 Add functionality to retrieve client secret expiry date… #39

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions spec/password_credentials_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "./spec_helper"

describe Office365::PasswordCredentials do
describe "#secret_expiry" do
it "should return secret expiry when present" do
SpecHelper.mock_client_auth
SpecHelper.mock_list_application

client = Office365::Client.new(**SpecHelper.mock_credentials)

expiry = client.secret_expiry
expiry.should_not be_nil
expiry.should eq(Time::Format::ISO_8601_DATE_TIME.parse("2025-03-12T23:44:20.176Z"))
end

it "should return nil when expiry data is not present" do
SpecHelper.mock_client_auth
SpecHelper.mock_list_application(false)

client = Office365::Client.new(**SpecHelper.mock_credentials)

expiry = client.secret_expiry
expiry.should be_nil
end
end
end
26 changes: 24 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ module SpecHelper
end

def mock_list_events
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/foo%40bar.com/calendar/calendarView?startDateTime=2020-01-01T00%3A00%3A00-00%3A00&endDateTime=2020-06-01T00%3A00%3A00-00%3A00").to_return(mock_event_query_json)
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/foo%40bar.com/calendar/calendarView?startDateTime=2020-01-01T00%3A00%3A00-00%3A00&endDateTime=2020-06-01T00%3A00%3A00-00%3A00&%24top=10000").to_return(mock_event_query_json)
end

def mock_list_events_error
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/bar%40foo.com/calendar/calendarView?startDateTime=2020-01-01T00%3A00%3A00-00%3A00&endDateTime=2020-06-01T00%3A00%3A00-00%3A00")
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/bar%40foo.com/calendar/calendarView?startDateTime=2020-01-01T00%3A00%3A00-00%3A00&endDateTime=2020-06-01T00%3A00%3A00-00%3A00&%24top=10000")
.to_return(mock_event_query_json_error)
end

Expand Down Expand Up @@ -396,6 +396,28 @@ module SpecHelper
def mock_delete_attachment
WebMock.stub(:delete, "https://graph.microsoft.com/v1.0/users/foo%40bar.com/calendar/events/1234/attachments/1234").to_return(body: "")
end

def mock_list_application(expiry = true)
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/applications%28appid%3D%27%7B%7B%40client_id%7D%7D%27%29?%24select=passwordCredentials")
.to_return(body: mock_pwd_cred_data(expiry).to_json)
end

def mock_pwd_cred_data(expiry = true)
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#applications(passwordCredentials)/$entity",
"passwordCredentials": [
{
"customKeyIdentifier": nil,
"displayName": "Some Name",
"endDateTime": expiry ? "2025-03-12T23:44:20.176Z" : nil,
"hint": "HIN",
"keyId": "0002dcce-acca-41b5-94d9-60932f151eaf",
"secretText": nil,
"startDateTime": "2023-03-13T23:44:20.176Z",
},
],
}
end
end

Spec.before_each do
Expand Down
2 changes: 2 additions & 0 deletions src/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require "./attachments"
require "./batch_request"
require "./subscriptions"
require "./odata"
require "./password_credentials"

module Office365
USERS_BASE = "/v1.0/users"
Expand All @@ -22,6 +23,7 @@ module Office365
include Office365::BatchRequest
include Office365::Subscriptions
include Office365::OData
include Office365::PasswordCredentials

LOGIN_URI = URI.parse("https://login.microsoftonline.com")
GRAPH_URI = URI.parse("https://graph.microsoft.com/")
Expand Down
2 changes: 1 addition & 1 deletion src/models/user_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Office365
include JSON::Serializable

property value : Array(User)

@[JSON::Field(key: "@odata.nextLink")]
property next_page_token : String?

Expand Down
22 changes: 22 additions & 0 deletions src/password_credentials.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "time"

module Office365::PasswordCredentials
def list_application_credentials
path = "/v1.0/applications(appid='{{@client_id}}')"
params = URI::Params.new({"$select" => ["passwordCredentials"]})
response = graph_request(graph_http_request(request_method: "GET", path: path, query: params))
return nil if response.body.try &.empty?
JSON.parse(response.body).as_h
end

def secret_expiry : Time?
response = list_application_credentials
return nil unless response.is_a?(Hash)
credential = response["passwordCredentials"].as_a?.try &.first?
return nil unless credential
end_date_time = credential.as_h["endDateTime"]
if time = end_date_time.as_s?
Time::Format::ISO_8601_DATE_TIME.parse(time)
end
end
end
Loading