2017-09-11 1 views
1

Azure AD 테넌트를 사용하여 android 용 AppAuth 테스트 앱을 만들었지 만 제대로 작동합니다. 이제 iOS (Swift 4)와 동일하게 노력 중이며 액세스 토큰에 대한 액세스 코드를 교환하려고 할 때 실패합니다. 오류가 반환되지 않습니다. idToken은 있지만 accessToken이나 refreshToken은 없습니다. 다른 오류는 없습니다. 무슨 일이 일어나고 있는지 잘 모르겠다. 액세스 토큰이 없으면 그래프를 쿼리 할 수 ​​없습니다. 저는 Azure AD v2를 사용하고 있습니다. 다음은 내 코드의 일부입니다.AppAuth iOS 토큰 교환 문제 Azure AD

func appAuthAuthorize(authConfig: AuthConfig) { 
    let serviceConfiguration = OIDServiceConfiguration(
     authorizationEndpoint: NSURL(string: authConfig.authEndPoint)! as URL, 
     tokenEndpoint: NSURL(string: authConfig.tokenEndPoint)! as URL) 

    let request = OIDAuthorizationRequest(configuration: serviceConfiguration, clientId: authConfig.clientId, scopes: [OIDScopeOpenID, OIDScopeProfile], redirectURL: NSURL(string: authConfig.redirectUri)! as URL, responseType: OIDResponseTypeCode, additionalParameters: nil) 

    doAppAuthAuthorization(authRequest: request) 
} 


func doAppAuthAuthorization(authRequest: OIDAuthorizationRequest) { 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 

    appDelegate.currentAuthorizationFlow = OIDAuthorizationService.present(authRequest, presenting: self, callback: { 
     (authorizationResponse, error) in 
     if (authorizationResponse != nil) { 
      self.authState = OIDAuthState(authorizationResponse: authorizationResponse!) 
      self.logMessage(message: "Got authorization code: \(String(describing: self.authState?.lastAuthorizationResponse.authorizationCode))") 
      self.doTokenRequest() 
     } else { 
      self.authState = nil 
      self.logMessage(message: "Authorization error: \(String(describing: error?.localizedDescription))") 
     } 
    }) 
} 

func doTokenRequest() { 
    let tokenExchangeRequest = authState?.lastAuthorizationResponse.tokenExchangeRequest() 

    OIDAuthorizationService.perform(tokenExchangeRequest!) { 
     tokenResponse, error in 
     if tokenResponse == nil{ 
      self.logMessage(message: "Token exchange error: \(error!.localizedDescription)") 
     } else { 
      self.authState?.update(with: tokenResponse!, error: error) 
      self.saveState() 
      self.logMessage(message: "Received token response with accesToken: \(tokenResponse!.idToken!)") 
      self.logMessage(message: "Received token response with accesToken: \(tokenResponse!.refreshToken!)") 
      self.logMessage(message: "Received token response with accesToken: \(tokenResponse!.accessToken!)") 
      self.retrieveUserProfile() 
     } 

     self.authState?.update(with: tokenResponse, error: error) 
    } 
} 
+0

당신이 PowerShell을 사용하여 그래프 API에 응용 프로그램을 등록 있나요? v1.6에서는이 작업을 수행해야하지만 v2에서는 필요하지 않은 작업입니다. – Pytry

+0

하지 않아도됩니다. 내 Android 앱 및 MSAL을 사용하는 iOS 예가 정상적으로 작동합니다. 그것의 유일한 AppAuth가 제대로 작동하지 않습니다. – Igor

+0

아, 그래. OpenIdConnect 편안한 플로우를 제공 할 수는 있지만 MSAL은 사용한 적이 없습니다. 죄송합니다. – Pytry

답변

1

답을 얻었다. 문제는 인증 서버에 따라 해당 서버에 대해 범위 범위를 정의해야한다는 것입니다. 위의 코드에서 OIDScopeOpenID 및 OIDScopeProfile의 기본 OpenId 범위를 사용했습니다. 이걸 Azure AD 범위의 User.Read로 변경하자마자 모든 것이 제대로 작동하기 시작했습니다. 그래서 여기에 기능 appAuthAuthorize의 코드에 순 변화는 다음과 같습니다

func appAuthAuthorize(authConfig: AuthConfig) { 
let serviceConfiguration = OIDServiceConfiguration(
    authorizationEndpoint: NSURL(string: authConfig.authEndPoint)! as URL, 
    tokenEndpoint: NSURL(string: authConfig.tokenEndPoint)! as URL) 

let request = OIDAuthorizationRequest(configuration: serviceConfiguration, clientId: authConfig.clientId, scopes: ["User.Read"], redirectURL: NSURL(string: authConfig.redirectUri)! as URL, responseType: OIDResponseTypeCode, additionalParameters: nil) 

doAppAuthAuthorization(authRequest: request) 

}

+0

토큰 교환 오류가 발생했습니다 : "작업을 완료 할 수 없습니다. OAuth 오류 : invalid_grant ", 비슷한 문제가 있습니까? – Jan

관련 문제