static void Main() { // This is the LMS we will interact with var host = new HostSpec( "https", "lms.valence.desire2learn.com", 443 ); // The appId/appKey come from our app.config - it is good to seperate access keys from the code that uses them. // Ideally you wouldn't have production keys committed to source control. string appId = ConfigurationManager.AppSettings["appId"]; string appKey = ConfigurationManager.AppSettings["appKey"]; // This is the port we will temporarily host a server on to intercept the user tokens after a successful login int port = int.Parse( ConfigurationManager.AppSettings["serverPort"] ); // Create url for the user to login. If they have already done so they will not actually have to type their password (maybe). var appContextFactory = new D2LAppContextFactory(); var appContext = appContextFactory.Create( appId, appKey ); var authUrl = appContext.CreateUrlForAuthentication( host, new Uri( "http://localhost:" + port + "/result/" ) ); OpenBrowser( authUrl ); // This call will block until we have a result // TODO: you'll want better control flow and error handling here var userContext = InterceptUserTokens( host, appContext ); // Now we can call Valence DoApiStuff( host.Scheme + "://" + host.Host + ":" + host.Port, userContext ); // Pause the terminal Console.ReadKey(); }
private static ID2LUserContext InterceptUserTokens( HostSpec host, ID2LAppContext appContext ) { // Start HTTP server and listen for the redirect after a successful auth var httpListener = new HttpListener(); httpListener.Prefixes.Add("http://localhost:31337/result/"); httpListener.Start(); // This call blocks until we get a response var ctx = httpListener.GetContext(); // The LMS returns the user tokens via query parameters to the value provided originally in x_target // TODO: deal with "failed to login" case var userContext = appContext.CreateUserContext( ctx.Request.Url, host ); // Send some JavaScript to close the browser popup // This is not 100% effective: for example, Firefox will ignore this. const string RESPONSE = "<!doctype html><meta charset=\"utf-8\"><script>window.close();</script><h1>You may now close your window</h1><p>You may or may not see this message, depending on your browser</p>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes( RESPONSE ); ctx.Response.ContentType = "text/html"; ctx.Response.ContentLength64 = buffer.Length; ctx.Response.OutputStream.Write( buffer, 0, buffer.Length ); ctx.Response.OutputStream.Close(); httpListener.Stop(); return userContext; }
public Valence() { var appFactory = new D2LAppContextFactory(); m_valenceAppContext = appFactory.Create(m_appId, m_appKey); m_valenceHost = new HostSpec("https", LMS_URL, 443); // Note Using existing authenicated Tokens m_valenceUserContext = m_valenceAppContext.CreateUserContext(m_userId, m_userKey, m_valenceHost); }
protected void Page_Load(object sender, EventArgs e) { if (Session["appID"] == null) { Session["appID"] = _defaultAppID; Session["appKey"] = _defaultAppKey; } appIDField.Text = Session["appID"].ToString(); appKeyField.Text = Session["appKey"].ToString(); hostField.Text = "valence.desire2learn.com"; if (!Page.IsPostBack) { var factory = new D2LAppContextFactory(); //assigns the AppID and AppKey into the AppID and AppKey in the AppContext var appContext = factory.Create(Session["appID"].ToString(), Session["appKey"].ToString()); int port; try { //try to read the port specified port = Convert.ToInt32(portField.Text); } catch (Exception error) { //fallback to https port port = 443; } //holds the scheme, hostname and which port to use HostSpec hostInfo = new HostSpec("https", hostField.Text, port); //saves the callback url as well as sets up the AppID, AppKey, UserID, and UserKey //if run without x_a, x_b, will be null ID2LUserContext userContext = appContext.CreateUserContext(Request.Url, hostInfo); if (userContext != null) { //This is how applications may want to store the info and handle authentication. This example must deal with changing information and therefore //cannot rely on an important variable being the same. Session["userContext"] = userContext; UserContextProperties properties = userContext.SaveUserContextProperties(); Session["userID"] = properties.UserId; Session["userKey"] = properties.UserKey; } } if (Session["userID"] != null) { userIDField.Text = Session["userID"].ToString(); userKeyField.Text = Session["userKey"].ToString(); } }
public HomeController( ) { var appFactory = new D2LAppContextFactory(); m_valenceAppContext = appFactory.Create( m_appId, m_appKey ); m_valenceHost = new HostSpec( "https", LMS_URL, 443 ); }
internal static ID2LUserContext AnonUserContext( HostSpec host ) { return m_appContext.CreateAnonymousUserContext( host ); }
protected void GetVersions(object sender, EventArgs e) { D2LAppContextFactory factory = new D2LAppContextFactory(); ID2LAppContext appContext = factory.Create(Session["appID"].ToString(), Session["appKey"].ToString()); int port; try { //try to read the port specified port = Convert.ToInt32(portField.Text); } catch (Exception) { //fallback to https port port = 443; } HostSpec hostInfo = new HostSpec("https", hostField.Text, port); //anonymous user context has the standard AppID/AppKey with null UserID/UserKey //grabs the apiHost information(scheme, hostname, port) and stores that as well ID2LUserContext userContext = appContext.CreateAnonymousUserContext( hostInfo); CallGetVersions(userContext, 2); }
protected void WhoAmI(object sender, EventArgs e) { //if the settings couldn't be changed between calls the next few steps would be replaced with using Session["userContext"] D2LAppContextFactory factory = new D2LAppContextFactory(); ID2LAppContext appContext = factory.Create(Session["appID"].ToString(), Session["appKey"].ToString()); int port; try { //try to read the port specified port = Convert.ToInt32(portField.Text); } catch (Exception error) { //fallback to https port port = 443; } String userID = "t"; String userKey = "t"; if (Session["userID"] != null) { userID = Session["userID"].ToString(); userKey = Session["userKey"].ToString(); } HostSpec hostInfo = new HostSpec("https", hostField.Text, port); ID2LUserContext userContext = appContext.CreateUserContext( userID, userKey, hostInfo); CallWhoAmI(userContext, 2); }
public ActionResult Index() { var param = Session[SESSION_KEY] as SessionParameters; if( param == null ) { ViewBag.ErrorMessage = "Unable to retrieve required session param."; return View( "BookError" ); } if ( param.LtiUri == null ) { ViewBag.ErrorMessage = "LTI param are not valid."; return View( "BookError" ); } // retrieve the required version information from the LMS var factory = new D2LAppContextFactory(); var appContext = factory.Create( m_defaultAppId, m_defaultAppKey ); var hostInfo = new HostSpec( param.LtiUri.Scheme, param.LtiUri.Host, param.LtiUri.Port ); ID2LUserContext context = appContext.CreateUserContext( Request.Url, hostInfo ); if( context == null ) { ViewBag.ErrorMessage = "Unable to create user context."; return View( "BookError" ); } param.UserContext = context; return RedirectToAction( "Assigned" ); }
private Uri GenerateAuthRedirect( Uri returnUri, Uri requestUri ) { if(( requestUri == null ) || ( returnUri == null )) { throw new ArgumentNullException(); } var factory = new D2LAppContextFactory(); var appContext = factory.Create( m_defaultAppId, m_defaultAppKey ); var resultUri = new UriBuilder( requestUri.Scheme, requestUri.Host, requestUri.Port, requestUri.AbsolutePath ).Uri; var host = new HostSpec( returnUri.Scheme, returnUri.Host, returnUri.Port ); var redirectUri = appContext.CreateUrlForAuthentication( host, resultUri ); return redirectUri; }